Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 10:03
    ruiluntran starred angular/angular
  • 09:52
    gkalpak commented #42598
  • 09:51
    gkalpak labeled #42598
  • 09:51
    gkalpak unassigned #42598
  • 09:43
    gkalpak commented #42598
  • 09:24
    gkalpak assigned #42598
  • 09:23
    gkalpak unlabeled #42584
  • 09:23
    gkalpak labeled #42584
  • 09:23
    gkalpak review_requested #42584
  • 08:57
    mary-poppins commented #42584
  • 08:56
    gkalpak labeled #42452
  • 08:56
    rdrygas starred angular/angular
  • 08:55
    gkalpak ready_for_review #42452
  • 08:54
    sasirekhamurakonda starred angular/angular
  • 08:46
    gkalpak synchronize #42584
  • 08:36
    mary-poppins commented #42452
  • 08:28
    gkalpak edited #42452
  • 08:27
    gkalpak unlabeled #42452
  • 08:26
    google-cla[bot] labeled #42452
  • 08:26
    google-cla[bot] unlabeled #42452
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
for both show same result
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 console.log('... adding Type property:', facility); shows only ... adding Type property: and nothing for facility?