How to create nested JSON depending on values in angular

I do an API call and then I received a JSON, I need to modify that JSON based on their values, for example: if an id is equal to 0

The only way I’ve found to accomplish this is by adding multiple if statements and I want to do it dynamically by a function

This is my JSON

   {
      "Last_Scanned": 1,
      "Prefix_Title": 1 ,
      "First_Name": 0,
      "Family_Name": 1,
      "Job_Title": 1, 
      "Company": 0,
      "Address_1": 1, 
      "Zip_Code": 1, 
      "City": 1,
      "Country_Code": 0
    }

An this is how I’m trying to filter by the ones that have a value equals to 0, the response is the original json

let selectedfield:any = [response]
        selectedfield.forEach(element => {
          avoidFields =  selectedfield.map((x:any) => { 
            if(x.Last_Scanned== 0){
              avoidFields.push(Object.keys(x.Person_Id))
              console.log(Object.keys(x))
            }

          })
        })

The result I’m looking for is for having something like this

   {
      "First_Name": 0,
      "Company": 0,
      "Country_Code": 0
    }

OR

   {
      "First_Name",
      "Company",
      "Country_Code"
   }

Answer

Does this satisfy what you’re trying to do?

// sample entry.
let sampleObject = {
  "Last_Scanned": 1,
  "Prefix_Title": 1 ,
  "First_Name": 0,
  "Family_Name": 1,
  "Job_Title": 1, 
  "Company": 0,
  "Address_1": 1, 
  "Zip_Code": 1, 
  "City": 1,
  "Country_Code": 0
};

// destination object.
let newObject = {};

// iterate through the object properties.
for (let prop in sampleObject) {
  // add to the new object if value is zero.
  if (sampleObject[prop] === 0) {
    newObject[prop] = 0; // or any value you want to set.
  }
}

console.log(newObject);