简体   繁体   English

循环遍历 2 个数组和对象数组,并将数组 1 和数组 2 的元素插入到对象的某些键的数组中

[英]loop through 2 arrays and array of objects and inserting the elements of array 1 and array 2 into the array of object's certain keys

I have an array of objects and an array.我有一个对象数组和一个数组。 My intention is to loop through both and inserting each element of the array into a certain key into the array of objects.我的目的是遍历两者并将数组的每个元素插入到对象数组中的某个键中。

My intention to dynamically insert the values of each array我打算动态插入每个数组的值

Notes for Clarification:澄清说明:

  1. arr1 length = 44 arr1 长度 = 44
  2. arr2 length = 44 arr2 长度 = 44
  3. arrOfObj length = 44 arrOfObj 长度 = 44
  4. my thought process of the operation:我的操作思路:

    1. loop through arr1 either by forEach or for通过 forEach 或 for 循环遍历 arr1
    2. loop through arr2循环通过 arr2
    3. loop through arrOfObject循环遍历 arrOfObject
    4. insert into arrOfObject[i].labels arr1[i]插入 arrOfObject[i].labels arr1[i]
    5. insert into arrOfObject[i].values arr2[i]插入 arrOfObject[i].values arr2[i]

an example:一个例子:

  • Array 1数组 1
    const arr = [ 76, 72, 69, 66, 66, 66, 65, 65, 64, 64, 64, 63, 61, 61, 61, 61, 61, 61, 60, 59, 59, 59, 58, 58, 57, 57, 56, 56, 56, 55, 54, 54, 53, 52, 52, 51, 51, 50, 50, 49, 49, 49, 47, 47]
  • Array 2阵列 2
    const arr2 = [ "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10", "Item11", "Item12", "Item13", "Item14", "Item15", "Item16", "Item17","Item18", "Item19", "Item20", "Item21", "Item22", "Item23", "Item24", "Item25","Item26", "Item27", "Item28", "Item29", "Item30", "Item31", "Item32", "Item33", "Item34", "Item35", "Item36", "Item37", "Item38", "Item39", "Item40", "Item41", "Item42", "Item43", "Item44]
  • Array of object对象数组
    const arrOfObj = [
    {
      labels:[],
      values: [],
    },
    {
      labels:[],
      values: [],
     },
    {
      labels:[],
      values: [],
     },
    ]

The desired output is所需的输出是

    const arrOfObj = [
    {
      labels:[arr1[0]],
      values: [arr2[0]],
    },
    {
      labels:[arr1[1]],
      values: [arr2[1]],
     },
    ]

-outcome -结果

    const arrOfObj = [
    {
      labels:['item1'],
      values: [76],
    },
    {
      labels:['item2'],
      values: [72],
     },
    ]

If there is a way to do it without nested loops (maybe one loop for values insertion and a separate one for labels), as nested loops decrease the execution performance, then it is preferable unless it is a necessity then it is okay.如果有一种方法可以在没有嵌套循环的情况下执行此操作(可能一个循环用于值插入,另一个用于标签插入),因为嵌套循环会降低执行性能,那么除非必要,否则最好。

As well if you a good source for objects and arrays manipulation above the basics level it will great to share同样,如果您是基础级别以上的对象和数组操作的良好来源,那么分享会很棒

so far my work can be viewed on https://codesandbox.io/s/epic-archimedes-8kg6h?eslint=1&expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark到目前为止,我的作品可以在https://codesandbox.io/s/epic-archimedes-8kg6h?eslint=1&expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark上查看

thanks a lot in advance非常感谢提前

Reworked the original answer to this function.重新编写了此功能的原始答案。 It will dynamically do what you intended for x array with x length它会动态地做你想要的 x 长度 x 数组

function combineArr(target, ...arrays){
    // looping through the target array
    target.map((ele, superIndex)=>{
        // looping through the attributes of the target
        Object.keys(ele).map((key, index)=>{
            // writing the results
            target[superIndex][key] = arrays[index][superIndex]
        })
    })
    return target;
}

console.log(combineArr(arrayOfObj,arr1,arr2))

target is your arrOfObj, and arrays will be arr1, arr2目标是你的 arrOfObj,数组将是 arr1, arr2

So what it boils down to is that your arrOfObj shares an index with arr1 and arr2.所以它归结为你的 arrOfObj 与 arr1 和 arr2 共享一个索引。 arrOfObj[i] arr1[i] arr2[i] but since arrOfObj[i] is an object with keys we make it iterable with Object.keys and make the arrays iterable aswell with ...arrays so that keys[i] shares index with arrays[i] arrOfObj[i] arr1[i] arr2[i] 但由于 arrOfObj[i] 是一个带有键的对象,我们使它可以用 Object.keys 迭代,并使数组也可以用 ...arrays 迭代,这样 keys[i] 共享索引使用数组[i]

Edit: I was able to reproduce the error that was present in your link.编辑:我能够重现您的链接中存在的错误。 The generation of arrOfObj with the template was broken(don't know why) to fix this and a little bit improved version of the combiner see below使用模板生成 arrOfObj 被破坏(不知道为什么)来解决这个问题,组合器的一点改进版本见下文

// use const when possible
const arr1 = [];
const arr2 = [];


// was fine as is 
// Getting the Keys and values of input in 2 separate arrays
inputs.forEach(x => {
    for (const key in x) {
        arr1.push(key);
        arr2.push(x[key]);
    }
});

//template made some problems... don't know why
// using arrOfObj to have template dubplicated the same number of arr1 and arr2
const arrOfObj = inputs.map(() => {
    return {
        labels: [],
        values: []
    }
})

// function declaration for combiner
function combineArr(target, ...arrays) {
    target.map((ele, superIndex) => {
        Object.keys(ele).map((key, index) => {
            target[superIndex][key].push(arrays[index][superIndex])
        })
    })
    return target;
}

// calling for new array
console.log(combineArr(arrOfObj, arr1, arr2))

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

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