如何在对象中的对象数组中检索特定的键值?
我有一个像这样的阵列OriginalArrayData:
://i.sstatic.net/h4jxh.png“ alt =“在此处输入图像描述”>
第一个数组项目的值(是对象)是几个对象。数组第一部分内容的一个示例就是这样:
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
}...........
假设我有一系列ID(这些数字可能是随机的,并且不总是2。可能有1、3等。数组项)
arrayOfIds: [16 , 17]
如果GRID_COL_ID的值在阵列ofids中的任何位置都存在,则如何从每个对象中检验“数据”值并将其放在其自己的数组中?
我知道如何从数组中的每个第一个对象中检索所有ID的数组:
let data = this.arrayList.map((obj) => obj.id);
以上产量:[5,6,7,8,9]。但是,现在我正在尝试的事情是正确的。到目前为止,我有以下内容
var targetArr = []
this.originalArrayData.forEach(item=> {
item.forEach(ins => {
if(arrayOfIds.includes(ins.grid_col_id)
targetArr.push(ins.data)
})
})
:出现错误:typeError:row.foreach不是
我的目标:[10,14,...]
目标包含10和14,因为如果您查看原始arraraydata,如果GRID_COL_ID(如果GRID_COL_ID)位于Arrayofids内部:[16,17],然后我们检索“数据”值并将其放入新数组中。
如何实现目标阵列?
I have an array originalArrayData like so:
Expanding:
The value of the first array item, which is an object, is several objects. An example of the contents of the first part of the array is as such:
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
}...........
Let's say I have an array of ids (these numbers could be random and there isn't always 2. There could be 1, 3, etc. array items)
arrayOfIds: [16 , 17]
If the value of grid_col_id is present anywhere in the arrayOfIds, how can I retrive the 'data' value from each object and put it in its own array?
I know how to retrieve an array of all ids from each first object within the array:
let data = this.arrayList.map((obj) => obj.id);
The above yields: [5,6,7,8,9]. However, it's now correct for what I am attempting. So far I have the following:
var targetArr = []
this.originalArrayData.forEach(item=> {
item.forEach(ins => {
if(arrayOfIds.includes(ins.grid_col_id)
targetArr.push(ins.data)
})
})
which yields an error: TypeError: row.forEach is not a function
My TARGET is: [10, 14, ...]
The target contains 10 and 14 because if you look at the originalArrayData, if grid_col_id is inside arrayOfIds: [16 , 17], then we retrieve the "data" value and put it inside a new array.
How can I achieve the target array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以按照以下操作。请参阅《代码中的评论》以获取说明
You can do as follows. See comments in the code for explanation
干得好:
Here you go:
这是一种更像您最初尝试的替代方法。
您可以使用 object.keys.keys.keys ,
Here's an alternative that more closely resembles your original attempt.
You can use Object.keys, Object.hasOwn and Array.prototype.find to get the key of the property with grid_col_id: