below is an array of object
const arr_obj = [
{
'id': 1,
'items': [
{
'id':'1',
'data': {
'id': 3,
}
},
{
'id': '2',
'data': {
'id': 4,
}
}
]
},
{
'id': 2,
'items': [
{
'id':'3',
'data': {
'id': 5,
}
},
]
},
]
I want to retrieve the id property of items array and put it an array so the expected output is ['1','2','3']
could someone help me with this. thanks.
arr_obj.map(obj=>obj.items.map(item=>item.id)).flat()
.map loops over every item in an array and runs a transformation function on them, generating a new array.
.flat flattens a multi-dimensional array into a 1d array
I'd suggest looking into some of the other array methods, as they are very useful.
You could even improve this slightly with flatMap:
arr_obj.flatMap(obj=>obj.items.map(item=>item.id))