如何在对象中的对象数组中检索特定的键值?

发布于 2025-02-09 11:33:29 字数 1677 浏览 1 评论 0原文

我有一个像这样的阵列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:

enter image description here

Expanding:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

メ斷腸人バ 2025-02-16 11:33:30

您可以按照以下操作。请参阅《代码中的评论》以获取说明

let od  = [{
    "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 filter = [16, 17];

//transform the original data into a flat array of objects
let dd = od.map(x => 
   //all property values of the object
   Object.values(x)
   //which are an object (ie filter away primitve types
   .filter(y => typeof y === "object")) 
   //flatten the array of arrays 
   .flat()
  
let result = dd  
   //filter the list of objects for the ids in the filter
  .filter(x => filter.includes(x.grid_col_id))
  //and get only the data property
  .map(x => x.data)

  console.log(result);

You can do as follows. See comments in the code for explanation

let od  = [{
    "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 filter = [16, 17];

//transform the original data into a flat array of objects
let dd = od.map(x => 
   //all property values of the object
   Object.values(x)
   //which are an object (ie filter away primitve types
   .filter(y => typeof y === "object")) 
   //flatten the array of arrays 
   .flat()
  
let result = dd  
   //filter the list of objects for the ids in the filter
  .filter(x => filter.includes(x.grid_col_id))
  //and get only the data property
  .map(x => x.data)

  console.log(result);

一身软味 2025-02-16 11:33:30

干得好:

const input=[{"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}];

const arrayOfIds = [16 , 17];

const format = (array) => {
    return array.reduce((result, el) => {
        const key = Object.keys(el).find((key) => 'object' === typeof el[key]);
        
        if(arrayOfIds.includes(+key)){
            result.push(+el[key].data);
        }

        return result;
    }, []);
};

console.log(format(input));

Here you go:

const input=[{"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}];

const arrayOfIds = [16 , 17];

const format = (array) => {
    return array.reduce((result, el) => {
        const key = Object.keys(el).find((key) => 'object' === typeof el[key]);
        
        if(arrayOfIds.includes(+key)){
            result.push(+el[key].data);
        }

        return result;
    }, []);
};

console.log(format(input));

情场扛把子 2025-02-16 11:33:30

这是一种更像您最初尝试的替代方法。

您可以使用 object.keys.keys.keys

const originalArrayData = [{
    "16": {
        "id": 22,
        "grid_row_id": 5,
        "grid_col_id": 16,
        "data": "10",
    },
    "header": "Row 2",
    "id": 5
},
{
    "17": {
        "id": 31,
        "grid_row_id": 9,
        "grid_col_id": 17,
        "data": "14",
    },
    "header": "Row 1",
    "id": 6
},
{
    "18": {
        "id": 35,
        "grid_row_id": 9,
        "grid_col_id": 12,
        "data": "55",
    },
    "header": "Row 1",
    "id": 6
}]

const arrayOfIds = [16, 17]
const targetArr = []

originalArrayData.forEach(d => {
    const keyOfObjectWithGridCol = 
        // Get all keys of the object as an array
        Object.keys(d)
        // Get the key of the nested object that has grid_col_id
        .find(key => Object.hasOwn(d[key], 'grid_col_id')) // Find property

    if (arrayOfIds.includes(d[keyOfObjectWithGridCol].grid_col_id)) {
        targetArr.push(d[keyOfObjectWithGridCol].data)
    }
})

console.log(targetArr) // [10, 14]

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:

const originalArrayData = [{
    "16": {
        "id": 22,
        "grid_row_id": 5,
        "grid_col_id": 16,
        "data": "10",
    },
    "header": "Row 2",
    "id": 5
},
{
    "17": {
        "id": 31,
        "grid_row_id": 9,
        "grid_col_id": 17,
        "data": "14",
    },
    "header": "Row 1",
    "id": 6
},
{
    "18": {
        "id": 35,
        "grid_row_id": 9,
        "grid_col_id": 12,
        "data": "55",
    },
    "header": "Row 1",
    "id": 6
}]

const arrayOfIds = [16, 17]
const targetArr = []

originalArrayData.forEach(d => {
    const keyOfObjectWithGridCol = 
        // Get all keys of the object as an array
        Object.keys(d)
        // Get the key of the nested object that has grid_col_id
        .find(key => Object.hasOwn(d[key], 'grid_col_id')) // Find property

    if (arrayOfIds.includes(d[keyOfObjectWithGridCol].grid_col_id)) {
        targetArr.push(d[keyOfObjectWithGridCol].data)
    }
})

console.log(targetArr) // [10, 14]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文