In my originalArrayData, I have an array structured as follows:
https://i.sstatic.net/soqgt.png
Expanding on this:
https://i.sstatic.net/H4jXh.png
The first item in the array is an object that contains multiple other objects. Here is an example of the contents of the array:
originalArrayData = [{
"16": {
"id": 22,
"grid_row_id": 5,
"grid_col_id": 16,
"data": "10",
"created_at": "rertte",
"error_mgs": null
},
"header": "Row 2",
"id": 5
},
{
"17": {
"id": 31,
"grid_row_id": 9,
"grid_col_id": 17,
"data": "14",
"created_at": "rtyhtyjtdyj",
"error_mgs": null
},
"header": "Row 1",
"id": 6
},
{
"18": {
"id": 35,
"grid_row_id": 9,
"grid_col_id": 12,
"data": "55",
"created_at": "thrtuhrs",
"error_mgs": null
},
"header": "Row 1",
"id": 6
}...........
Assume I have an array of ids, represented as follows (the numbers can vary and there may be 1, 3, or more items):
arrayOfIds: [16 , 17]
If the value of grid_col_id matches any value in arrayOfIds, how can I extract the 'data' value from each object and store it in a new array?
I already know how to extract an array of all ids from each first object in the array:
let data = this.arrayList.map((obj) => obj.id);
The result is: [5,6,7,8,9]. However, this is not what I need for my current task. So, I have the following approach:
var targetArr = []
this.originalArrayData.forEach(item=> {
item.forEach(ins => {
if(arrayOfIds.includes(ins.grid_col_id)
targetArr.push(ins.data)
})
})
This results in an error message: TypeError: row.forEach is not a function
My GOAL is: [10, 14, ...]
The target array includes 10 and 14 because, based on the originalArrayData, if grid_col_id matches any value from arrayOfIds, we extract the "data" value and place it into a new array.
How can I achieve this target array?