简体   繁体   English

无法将第二个数组对象值添加到 javascript 中的第一个对象数组中

[英]Unable to add second array object value into first array of objects in javascript

hi i have two arrays i want to merge two arrays, can any one help one please, i have for used for loop pushing that values but its creating duplicates.嗨,我有两个数组,我想合并两个数组,任何人都可以帮忙吗,我有 for 循环推送这些值,但它会创建重复项。

  const firstArray = [
    {
      first: "01",
      data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }]
    },
    {
      first: "02",
      data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }]
    },
    {
      first: "03",
      data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }]
    }
  ];
  const secondArray = [{ data: "abc" }, { data: "efg" }, { data: "hij" }];

I need result like this can any one,我需要这样的结果,任何人都可以,

[
  {
    first: "01",
    data: [
      { id: "012345", data: "abc" },
      { id: "0123456", data: "efg" },
      { id: "0123457", data: "hij" },
    ],
  },
  {
    first: "02",
    data: [
      { id: "9998989", data: "abc" },
      { id: "1223", data: "efg" },
      { id: "345666", data: "hij" },
    ],
  },
  {
    first: "03",
    data: [
      { id: "567888", data: "abc" },
      { id: "2345", data: "efg" },
      { id: "09876", data: "hij" },
    ],
  },
];

Rather than using nested for loops to build your array from scratch, you can instead use two .map() methods.您可以使用两个.map()方法,而不是使用嵌套的 for 循环从头开始构建数组。 One for mapping your outer objects in firstArray to new objects with a new data value, and another for mapping your data to a merged version from the data in secondArray .一个用于将firstArray的外部对象映射到具有新data值的新对象,另一个用于将您的datasecondArray的数据映射到合并版本。 To merge, you can take the corresponding object from secondArray using the index and then using the spread syntax to merge both objects together:要合并,您可以使用索引从secondArray获取相应的对象,然后使用扩展语法将两个对象合并在一起:

 const firstArray = [ { first: "01", data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }] }, { first: "02", data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }] }, { first: "03", data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }] } ]; const secondArray = [{ data: "abc" }, { data: "efg" }, { data: "hij"}]; const res = firstArray.map(obj => ({ ...obj, data: obj.data.map((inner, i) => ({...inner, ...secondArray[i]})) })); console.log(res);

To fix your current implementation, you can create two arrays, one outside of your for loop like you currently are already doing, and then one inside your first for loop.要修复您当前的实现,您可以创建两个数组,一个在您的 for 循环之外,就像您目前已经在做的那样,然后一个在您的第一个 for 循环中。 The inner array will represent your new data array, which you can then update for your current object:内部数组将代表您的新数据数组,然后您可以为当前对象更新它:

 const firstArray = [{ first: "01", data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }] }, { first: "02", data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }] }, { first: "03", data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }] } ]; const secondArray = [{ data: "abc" }, { data: "123" }, { data: "xyz" }]; const emptyArray = []; for (let i = 0; i < firstArray.length; i++) { const data = firstArray[i].data; const dataArray = []; for (let j = 0; j < data.length; j++) { const newObject = { ...data[j], ...secondArray[j] }; dataArray.push(newObject); } emptyArray.push({...firstArray[i], data: dataArray}); } console.log(emptyArray);

Assuming the data array and second array size are same.假设data数组和第二个数组大小相同。 Use nested map method calls to build the aggregation使用嵌套的map方法调用来构建聚合

 const firstArray = [ { first: "01", data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }], }, { first: "02", data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }], }, { first: "03", data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }], }, ]; const secondArray = [{ data: "abc" }, { data: "efg" }, { data: "hij" }]; const output = firstArray.map(({ first, data }) => ({ first, data: data.map((item, i) => ({ ...item, ...secondArray[i] })), })); console.log(output)

You can easily achieve the result using map in js您可以在js中使用map轻松实现结果

 const firstArray = [ { first: "01", data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }], }, { first: "02", data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }], }, { first: "03", data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }], }, ]; const secondArray = [{ data: "abc" }, { data: "efg" }, { data: "hij" }]; const result = firstArray.map((obj) => ({ ...obj, data: obj.data.map((o, i) => ({ ...o, ...secondArray[i] })), })); console.log(result);

according to your desired result you should do something like this:根据您想要的结果,您应该执行以下操作:

let emptyArrray = [];
  for (let i = 0; i < firstArray.length; i++) {
    const dataa = firstArray[i].data;
    for (let j = 0; j < dataa.length; j++) {
      
        // console.log(secondArray[i]);
        let newObject = {
          ...secondArray[j],
          ...dataa[j]
        };
        emptyArrray.push(newObject);
      
    }
  } 

in your code you are getting 3*9 long array because you use nestled loop where you shouldn't, you just run over the array once, and add members from array two, also you used在您的代码中,您将获得 3*9 长数组,因为您在不应该使用的嵌套循环中使用了嵌套循环,您只需遍历该数组一次,然后添加数组 2 中的成员,您也使用过

  ...secondArray[i],
  ...dataa[i]

instead of代替

  ...secondArray[j],
  ...dataa[j]

when incrementing j in the for loop在 for 循环中增加j

暂无
暂无

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

相关问题 通过比较属性将第二个数组与第一个数组合并,如果对象不是第二个数组的一部分,则将属性添加到第一个数组中的对象 - merge second array with first array by comparing properties and add properties to objects in first array if object is not part of second array 如何使用angular6在基于id值的第一个数组对象中添加第二个数组对象? - How to add second array objects in first array object based on id value using angular6? 在Objects Javascript数组中添加一个Object - Add an Object in an array of Objects Javascript 从字符串 id 数组创建对象数组,第一个对象的第一个值是第二个,第二个是第三个,依此类推 - create array of objects from an array of string ids with 1st object having first value with second, second with third and so on javascript 用第二个数组值替换第一个数组值。 第一个数组长度为 2,第二个数组长度为 7 - javascript replace first array value with second array value. First array length is 2 and second one length is 7 将项目添加到对象数组中的第一个对象 - Add item to the first object inside an array of objects 基于第一个值作为键和第二个值作为javascript中的值创建一个对象数组 - Create an array of object based on first value as key and second value as value in javascript 无法访问第二个 javascript 文件中第一个 JavaScript 文件的数组内容 - Unable to access array content of first JavaScript file in second javascript file 写一个 function 来识别第一个数组中的对象,它匹配第二个 object 中的所有属性和值对 - Write a function to identify objects in the first array, which matches all the attribute and value pairs in the second object 在数组javascript中为对象添加值 - add value to object in array javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM