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
You can just
filter
the entries indata
based on whether thefirst
andlast
name fields are the same:Just loop through the keys and delete when the full name hasn’t changed:
use the for (const key in data) {} to get each rows and them compare the row data