How To Traverse Through Array Within An Object And Return Each Array Element As The Second Key Value Pair, Along With Its' Corresponding Id
I have an object that looks like this: { id: '100', domains: [ 'www.abc.com', 'www.def.com' ] } { id: '101', domains: [ 'ghi.com' ] }
Solution 1:
You can use Array.prototype.flatMap for this to map the elements to an array of results which will then be flattened together:
const data = [{
id: '100',
domains: [
'www.abc.com',
'www.def.com'
]
},
{
id: '101',
domains: ['ghi.com']
},
{
id: '102',
domains: ['www.jkl.com']
},
];
const result = data.flatMap(obj =>
obj.domains.map(url => ({
id: obj.id,
url
})));
console.log(result);
Post a Comment for "How To Traverse Through Array Within An Object And Return Each Array Element As The Second Key Value Pair, Along With Its' Corresponding Id"