How can I add new object if value exist
const data = [
{ id: '30', mom: 'A', code: '0141', 'child':' Not used', 'status' :' X' },
{ id: '31', mom: 'B', code: '0141', 'child':' add Child', 'status' :' Y' }
{ id: '32', mom: 'B', code: '0141', 'child':' add child 2', 'status' :' Y' }
]
first I map data and if status = X I push new array if not I want to add new object key call child in to array that've status = X
data.map((element, i) => {
if (element.status == 'X') {
arr.push({
id: element.id,
mom: element.mom,
code: element.code
});
} else {
// I want to add element child into arr that has status = X
arr.push({ child: element.child });
}
})
I want output like this
const arr = [
{
id: '30', mom: 'A', code: '0141',
child: [
'name':'add child',
'name':'add child2'
]
},
]
