简体   繁体   English

如何根据对象数组的值创建一个新的项目数组?

[英]How to create a new array of items based on the values of an array of objects?

I have an array of objects called firstArray with an unknown number of items:我有一个名为firstArray的对象数组,其中包含未知数量的项目:

firstArray = [
    {
        "id": 22,
        "col_id": 16,
        "data": "Once upon a star"
    },
    {
        "id": 27,
        "col_id": 17,
        "data": "10"        
    },
    {
        "id": 40,
        "col_id": 19,
        "data": "80"
    },
    {
        "id": 53,
        "col_id": 17,
        "data": "32"        
    },
    ....
]

I have another array of objects called toBeKeysArray , also an unknown number (it could be 2 as in the code below or 30):我有另一个名为toBeKeysArray的对象数组,也是一个未知数(它可能是 2,如下面的代码或 30):

toBeKeysArray = [
    {
        "seriesTempHeader": "DEER LAST",
        "id": 17
    },
    {
        "seriesTempHeader": "BLUE ITEM",
        "id": 16
    },
    ......
]

I want to create a new array based on the data in the firstArray and toBeKeysArray.我想根据 firstArray 和 toBeKeysArray 中的数据创建一个新数组。

TARGET ACCORDING TO ABOVE EXAMPLE:根据上述示例的目标:

  [{ "deerlast": 42, "blueitem": 80, "category": "None"}]

EXPLANATION: for each object in the firstArray with 'col_id' = 'id' of an item in toBeKeysArray, add the value of 'data' and equal it to the key of a new array where the key is the lowercase and no space version of seriesTempHeader in the right item in toBeKeysArray.解释:对于 firstArray 中的每个对象,其中 'col_id' = 'id' 在 toBeKeysArray 中的一个项目,添加 'data' 的值并将其等于新数组的键,其中键是小写且没有空格版本seriesTempHeader 在 toBeKeysArray 的右侧项目中。

So, in the new array we create the key 'deerlast' and 'blueitem' since they are in toBeKeysArray .因此,在新数组中,我们创建了键 'deerlast' 和 'blueitem',因为它们位于toBeKeysArray中。 'deerlast' has value 42 because in the firstArray , 2 items have col_id 17, which is the id of 'DEER LAST' in toBeKeysArray. 'deerlast' 的值为 42,因为在firstArray中,2 个项目的col_id 为17,这是 toBeKeysArray 中的 'DEER LAST' 的 id。 As such, we add the 'data' value of those two items.因此,我们添加了这两项的“数据”值。

For 'blueitem', the only object in firstArray to have col_id = 16 is the one with 'data' = 80.对于“blueitem”, firstArray中唯一一个 col_id = 16 的对象是“data” = 80 的对象。

Please note that "category": "None" is always going to be part of the target array and can be hardcoded.请注意 "category": "None" 总是成为目标数组的一部分并且可以被硬编码。

So far, I have the following:到目前为止,我有以下内容:

        // Retrieve the ids of toBeKeysArray
        let targetIds = toBeKeysArray.map((obj) => obj.id);

        var targetArray = ["category": "None"]

        // For each item in the first array, if the col_id is part of the targetIds, populate the new array
        firstArray.forEach(element => {
            if(targetIds.includes(element.col_id)) {
                this.targetArray.push("toBeKeysArray.seriesTempHeader": +data)
            }
        });

However, it does not result in the correct solution.但是,它不会导致正确的解决方案。 Based on the information above, how would I be able to get the target array?基于以上信息,我将如何获得目标数组?

You map to iterate over the toBeKeysArray and then you search the firstArray by filtering out the ones that don't match the col_id.您映射以迭代 toBeKeysArray,然后通过过滤掉与 col_id 不匹配的数组来搜索 firstArray。

PS.: The numbers.filter with the !isNaN(...) is due to one of the datas being a string. PS.:带有 !isNaN(...) 的 numbers.filter 是由于其中一个数据是字符串。 You may need to tweak some of the code here, but I think this is the main idea您可能需要在这里调整一些代码,但我认为这是主要思想

const result = toBeKeysArray.map((key) => {

   const numbers = firstArray.filter(data => data.col_id === key.id);
   
    return {
     [key.seriesTempHeader]: numbers.filter(number => !isNaN(parseInt(number.data, 10))).reduce((acc, number) => acc + parseInt(number.data, 10), 0)
  };

});

console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM