简体   繁体   English

推送另一个数组中每个对象内的每个对象的元素

[英]push elements of each object inside each object inside another array

I have two arrays, one is my original one called data which consists of : 我有两个数组,一个是我原来的一个data ,它包括:

const datas = [
  {
    name: 'core Test',
    item: [
      {
        name: 'test/core/core.js',
        item: "item1"
      }
    ]
  },
  {
    name: 'users Test',
    item: [
      {
        name: 'test/users/user.js',
        item: "item2"
      }
    ]
  }
]

And i have another array called replace , which i'm trying to push each of its elements inside my original one, inside the 我有另一个名为replace数组,我试图将其中的每个元素推入原始元素中

const replace = [
  {
    type: "test1",
    number: "1",
  },
  {
    type: "test2",
    number: "2",
  }
]

Here is my code : 这是我的代码:

const transformedData = datas.map(data => {
  data.item = data.item.map(x => ({
    name: x.name,
    type: replace.map(y=>{return y;})
  }))
  return data
})

The output i get : 输出我得到:

[
  {
    "name": "core Test",
    "item": [
      {
        "name": "test/core/core.js",
        "type": [
          { "type": "test1", "number": "1" },
          { "type": "test2", "number": "2" }
        ]
      }
    ]
  },
  {
    "name": "users Test",
    "item": [
      {
        "name": "test/users/user.js",
        "type": [
          { "type": "test1", "number": "1" },
          { "type": "test2", "number": "2" }
        ]
      }
    ]
  }
]

The output i want : 我想要的输出:

[
  {
    "name": "core Test",
    "item": [
      {
        "name": "test/core/core.js",
        "type": { "type": "test1", "number": "1" }
      }
    ]
  },
  {
    "name": "users Test",
    "item": [
      {
        "name": "test/users/user.js",
        "type": { "type": "test2", "number": "2" }
      }
    ]
  }
]

This is because you're mapping all the way through the replace array every single time for each time you're inside of a value inside of datas. 这是因为每次进入数据内部的值时,每次都会一直映射到替换数组。 Instead you want to keep track of the index with your original map so then you only have one instance each time. 相反,您希望使用原始地图跟踪索引,以便每次只有一个实例。

Try something like: 尝试类似的东西:

const transformedData = datas.map((data, index) => {
  data.item = data.item.map(x => ({
    name: x.name,
    type: replace[index]
  }))
  return data;
});

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

相关问题 如何使用 foreach 将 object 中的每个数组值推送到另一个带有 vue 的数组? - How to push each value of array inside object to another array with vue using foreach? 在 json 数组内的每个 json 对象中添加另一个 json 对象 - Add another json object in each json object inside json array 将对象推入$ .each中的数组 - push Object in array in $.each 对于对象内的每个属性 - For each property inside an object Js为每个循环更改数组中的对象 - Js change object inside array in for each loop 替换数组中每个对象的值 - replace value of each object inside array 如何将元素推入 Mongoose 中另一个数组中的 object 中的数组? - How to push an element into an array inside an object inside another array in Mongoose? 我有一个对象数组,每个对象内都有一个称为分数的键,我需要找到每个分数的总和并将其推入数组 - I have an array of objects, inside each object is key called scores, and I need to find the sum of each score and push it into an array 根据每个数组内的值对对象内的数组进行排序 - Sort arrays inside object based on value inside each array (javascript)是否可以在位于另一个 object 内的阵列中推送 object? - (javascript)Is it possible to push an object in the array located inside another object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM