Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 20:51
    mary-poppins commented #42584
  • 20:51
    ManuelSzecsenyi starred angular/angular
  • 20:43
    gkalpak synchronize #42584
  • 20:42
    mary-poppins commented #42584
  • 20:40
    gkalpak labeled #42584
  • 20:32
    gkalpak synchronize #42584
  • 20:32
    pavanmatkar123 starred angular/angular
  • 20:12
    spravil starred angular/angular
  • 19:55
    renovate[bot] synchronize #42589
  • 19:55

    renovate[bot] on patch-all-minor-patch

    build: update dependency typesc… (compare)

  • 19:55
    renovate[bot] edited #42589
  • 19:55
    renovate[bot] edited #42589
  • 19:53
    alxhub synchronize #42485
  • 19:25
    k-matti starred angular/angular
  • 19:10
    flash-me commented #42591
  • 19:10
    flash-me commented #42591
  • 18:59
    petebacondarwin unlabeled #42593
  • 18:59
    petebacondarwin labeled #42593
  • 18:58
    petebacondarwin commented #42593
  • 18:53
    petebacondarwin unlabeled #42062
Miloš Lapiš
@mlc-mlapis
image.png
Aishasadat123
@Aishasadat123
var code = "formatted"; {
"facilityName": {
"name": "ODK"
},
"FacilityCode": "300",
"Type": "NPM",
"facilityType": {
"english": "Regional",
"dari": " ملی",
"pashto": "سیمه ایز"
}
},
{
"facilityName": {
"name": "POI"
},
"FacilityCode": "300",
"facilityType": {
"english": "Regional",
"dari": " ملی",
"pashto": "سیمه ایز"
}
}
Miloš Lapiš
@mlc-mlapis
image.png
Aishasadat123
@Aishasadat123
@mlc-mlapis it does not work for me
Miloš Lapiš
@mlc-mlapis
Do you have a problem with entering 3 back-ticks on separate lines?
It works, you can be sure.
Aishasadat123
@Aishasadat123
{
    "facilityName": {
        "name": "ODK"
    },
    "FacilityCode": "300",
    "Type": "NPM",
    "facilityType": {
        "english": "Regional",
        "dari": " ملی",
        "pashto": "سیمه ایز"
    }
},
{
    "facilityName": {
        "name": "POI"
    },
    "FacilityCode": "300",
    "facilityType": {
        "english": "Regional",
        "dari": " ملی",
        "pashto": "سیمه ایز"
    }
in this json object i want if there is no type property it sould add
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Done finally. :thumbsup:
Aishasadat123
@Aishasadat123
@mlc-mlapis yes
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Do you mean that you want to find out somehow if there is a property called add in any nested level?
Aishasadat123
@Aishasadat123
@mlc-mlapis yes i want to check each object if there is not type it add the type property
Aishasadat123
@Aishasadat123
in above code the first object has type property but the secound does not
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 So it's just JS thing, not Angular specific. A method of how to do it depends on the number of different combinations where add property can be placed. If it is a concrete place use a standard addressing path and either get the property or you get undefined. Otherwise use probably RegExp for searching.
Aishasadat123
@Aishasadat123
@mlc-mlapis ok
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Or go through the array in a cycle (for (...)) and simple check if the current element has Type property.
Aishasadat123
@Aishasadat123
'use strict';

const fs = require('fs');

fs.readFile('facility.json', (err, data) => {
    if (err) throw err;
    let facility = JSON.parse(data);
    console.log(facility);
});

let data = JSON.stringify(facility);
fs.writeFileSync('facility-2.json', data);
@mlc-mlapis in this code where i apply the for loop?
Miloš Lapiš
@mlc-mlapis
let facility = JSON.parse(data); // Your code line
for (let i = 0; i < facility.length; i++) {
   const element = facility[i];
   if (!element.Type) {
      // The element doesn't contain property of Type
   }
}
Aishasadat123
@Aishasadat123
@mlc-mlapis please edit my code
and i want to check if there is not type proprty it have to add the type property
Miloš Lapiš
@mlc-mlapis
Better naming:
let facilities = JSON.parse(data); // Your code line
for (let i = 0; i < facilities.length; i++) {
   const facility = facilities[i];
   if (!facility.Type) {
      // The element doesn't contain property of Type
      facility.Type = 'NPM';
   } else {
      console.log('... data type:', typeof facility.Type); // Would be a string data type
   }
}
Aishasadat123
@Aishasadat123
@mlc-mlapis in the (if ) how i say that type to those object ?
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 But be careful because fs.readFile is an async operation and fs.writeFileSync` is a sync operation, and you call that sync operation outside of the async body.
Aishasadat123
@Aishasadat123
'use strict';

const fs = require('fs');

fs.readFile('facilities.json', (err, data) => {
    if (err) throw err;

    let facilities = JSON.parse(data); // Your code line
    for (let i = 0; i < facilities.length; i++) {
        const facility = facilities[i];
        if (!facility.Type) {
            // The element doesn't contain property of Type
            facility.Type = 'NPM';
        } else {
            console.log('... data type:', typeof facility.Type); // Would be a string data type
        }
    }
    console.log(facilities);
});

let data = JSON.stringify(facilities);
fs.writeFileSync('facility-2.json', data);
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Then fs.readFileSync would be necessary or placing writing into the body of that async.
Aishasadat123
@Aishasadat123
my final code
it shows an error that facilities in 15 is not define
let data = JSON.stringify(facilities);
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 It can't work correctly because you have fs.writeFileSync outside of the async body.
@Aishasadat123 Sure because the declaration of facilities is inside the async block.
fs.readFile('facilities.json', (err, data) => {
    if (err) throw err;

    const facilities = JSON.parse(data); // Your code line
    for (let i = 0; i < facilities.length; i++) {
        const facility = facilities[i];
        if (!facility.Type) {
            // The element doesn't contain property of Type
            facility.Type = 'NPM';
        } else {
            console.log('... data type:', typeof facility.Type); // Would be a string data type
        }
    }
    console.log(facilities);

    const outputData = JSON.stringify(facilities);
    fs.writeFileSync('facility-2.json', outputData);
});
Aishasadat123
@Aishasadat123
now it shows this error
SyntaxError: Identifier 'data' has already been declared
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Sure, use outputData.
Aishasadat123
@Aishasadat123
ok
@mlc-mlapis it find the facility with type NPM
it just find thefacility with NPM type not add anything
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Try facility['Type'] = 'NPM'; instead of facility.Type = 'NPM';
Aishasadat123
@Aishasadat123
not working
the result is same
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Add:
if (!facility.Type) {
   // The element doesn't contain property of Type
   facility.Type = 'NPM';
   console.log('... adding Type property:', facility);
} else {
   console.log('... data type:', typeof facility.Type); // Would be a string data type
}
Aishasadat123
@Aishasadat123
@mlc-mlapis still same result
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 What is shown on the console for facility then?
Aishasadat123
@Aishasadat123
it show all facilities with type property
it does not make any change in the facility which does not has type property
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 I mean the result of: console.log('... adding Type property:', facility);
Aishasadat123
@Aishasadat123
... data type: string
... adding Type property
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Nothing for both syntax: facility.Type = 'NPM'; or facility['Type'] = 'NPM';?
Aishasadat123
@Aishasadat123
it show this