skip to Main Content

How do I loop through a nested object of the form

{
  'AAA': 
   { oldName: { index: 0, first: 'Bob', last: 'Hanson' },
     newName: { index: 0, first: 'Bob', last: 'Hanson' }
   }, 
  'BBB': 
   { oldName: { index: 1, first: 'Velma', last: 'Jones' },
     newName: { index: 2, first: 'Velma', last: 'Roberts'} 
   },
  'CCC': 
   { oldName: { index: 2, first: 'Jou', last: 'Xi' },
     newName: { index: 3, first: 'Jou', last: 'Xi' } 
   },
  'DDD': 
   { oldName: { index: 3, first: 'Betty', last: 'Ford' },
     newName: undefined},
  'EEE': 
   { oldName: undefined,
     newName: { index: 1, first: 'Fred', last: 'Tree' } 
  } 
}

and delete all the nested objects where for each first level key AAA, BBB, CCC, DDD, EEE
the value of the key oldName = the value of key newName

So, I end up with

{
 'BBB': 
   { oldName: { index: 1, first: 'Velma', last: 'Jones' },
     newName: { index: 2, first: 'Velma', last: 'Roberts' } 
   },
  'DDD': 
   { oldName: { index: 3, first: 'Betty', last: 'Ford' },
     newName: undefined },
  'EEE': 
   { oldName: undefined,
     newName: { index: 1, first: 'Fred', last: 'Tree' } 
   } 
}

3

Answers


  1. You can just filter the entries in data based on whether the first and last name fields are the same:

    const data = {
      'AAA': 
       { oldName: { index: 0, first: 'Bob', last: 'Hanson' },
         newName: { index: 0, first: 'Bob', last: 'Hanson' }
       }, 
      'BBB': 
       { oldName: { index: 1, first: 'Velma', last: 'Jones' },
         newName: { index: 2, first: 'Velma', last: 'Roberts'} 
       },
      'CCC': 
       { oldName: { index: 2, first: 'Jou', last: 'Xi' },
         newName: { index: 3, first: 'Jou', last: 'Xi' } 
       },
      'DDD': 
       { oldName: { index: 3, first: 'Betty', last: 'Ford' },
         newName: undefined},
      'EEE': 
       { oldName: undefined,
         newName: { index: 1, first: 'Fred', last: 'Tree' } 
      } 
    }
    
    const result = Object.fromEntries(
      Object.entries(data)
        .filter(([_, v]) => v.oldName?.first != v.newName?.first || v.oldName?.last != v.newName?.last)
    )
    
    console.log(result)
    Login or Signup to reply.
  2. Just loop through the keys and delete when the full name hasn’t changed:

    const data = {
      'AAA': 
       { oldName: { index: 0, first: 'Bob', last: 'Hanson' },
         newName: { index: 0, first: 'Bob', last: 'Hanson' }
       }, 
      'BBB': 
       { oldName: { index: 1, first: 'Velma', last: 'Jones' },
         newName: { index: 2, first: 'Velma', last: 'Roberts'} 
       },
      'CCC': 
       { oldName: { index: 2, first: 'Jou', last: 'Xi' },
         newName: { index: 3, first: 'Jou', last: 'Xi' } 
       },
      'DDD': 
       { oldName: { index: 3, first: 'Betty', last: 'Ford' },
         newName: undefined},
      'EEE': 
       { oldName: undefined,
         newName: { index: 1, first: 'Fred', last: 'Tree' } 
      } 
    }
    
    for (const key in data) {
      const {oldName: a, newName: b} = data[key];
      a?.first === b?.first && a?.last === b?.last && delete data[key];
    }
    
    console.log(data);
    Login or Signup to reply.
  3. use the for (const key in data) {} to get each rows and them compare the row data

    const data = {
        'AAA': 
         { oldName: { index: 0, first: 'Bob', last: 'Hanson' },
           newName: { index: 0, first: 'Bob', last: 'Hanson' }
         }, 
        'BBB': 
         { oldName: { index: 1, first: 'Velma', last: 'Jones' },
           newName: { index: 2, first: 'Velma', last: 'Roberts'} 
         },
        'CCC': 
         { oldName: { index: 2, first: 'Jou', last: 'Xi' },
           newName: { index: 3, first: 'Jou', last: 'Xi' } 
         },
        'DDD': 
         { oldName: { index: 3, first: 'Betty', last: 'Ford' },
           newName: undefined},
        'EEE': 
         { oldName: undefined,
           newName: { index: 1, first: 'Fred', last: 'Tree' } 
        } 
      }
      
    
    
    for (const key in data) {
        const {oldName, newName} = data[key];
        oldName?.first === newName?.first && oldName?.last === newName?.last && delete data[key];
    }
      
    console.log(data);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search