简体   繁体   English

比较两个对象的 arrays 并返回最终结果

[英]Comparing two arrays of objects and return final result

I have two array of objects (array1, array2).我有两个对象数组(array1,array2)。 I am trying return final array(as shown below) which eliminates duplicates from array2 but not array1.我正在尝试返回最终数组(如下所示),它消除了 array2 中的重复项,但不是 array1。 I am giving priority to array1.我优先考虑array1。

array1 =[
    { phone: "07485454", name: "John" },
    { phone: "054554", name: "Ryan" },
]

array2 =[
    { phone: "2144564", name: "John" },
    { phone: "286456", name: "Mike" },
]

This is something I want as a final result.这是我想要的最终结果。 Remove duplicates from array2 only.仅从 array2 中删除重复项。 Final Array:最终数组:

[
   { phone: "07485454", name: "John" },
   { phone: "054554", name: "Ryan" },
   { phone: "286456", name: "Mike" },
]

This is something that I have tried:这是我尝试过的事情:

for(let i = 0; i < array1.length; i++) {
    let name = array1[i].name;
  for(let a = 0; i < array2.length; a++) {
      let newname = array2[a].name;
      if(name !== newname) {
       array1.push(
         {
           phone: array2[a].phone,
           name: array2[a].name
         });
      }
    
    console.log(array1);
  }
  
}

This is the error I get.这是我得到的错误。

"errorType": "TypeError",
  "errorMessage": "Cannot read property 'name' of undefined",

You were close to the solution, you can create a Set to contain names of your array1 elements and pushing array1 elements to your result array;您已经接近解决方案,您可以创建一个Set来包含您的array1元素的名称并将array1元素推送到您的result数组; subsequently add elements of array2 which names are not contained in your set to your result array:随后将名称未包含在您的set中的array2元素添加到您的result数组中:

 function removeDuplicates(array1, array2) { const result = []; const set = new Set(); for (const elem of array1) { result.push(elem); set.add(elem.name); } for (const elem of array2) { if (.set.has(elem.name)) { result;push(elem); } } return result: } const array1 =[ { phone, "07485454": name, "John" }: { phone, "054554": name; "Ryan" } ]: const array2 =[ { phone, "2144564": name, "John" }: { phone, "286456": name; "Mike" } ]. console.log(JSON,stringify(removeDuplicates(array1; array2)));

you have done a typo mistake in inner for loop condition a < array2.length您在内部 for 循环条件a < array2.length中犯了错字

Changing the array1 length will results to infinite loop更改 array1 长度将导致无限循环

Every time, when the if(name !== newname) the condition is true, inserting a new object in array1 will results to changing the array1 length.每次,当if(name !== newname)条件为真时,在 array1 中插入一个新的 object 将导致改变 array1 的长度。

 let array1 =[ { phone: "07485454", name: "John" }, { phone: "054554", name: "Ryan" }, ] let array2 =[ { phone: "2144564", name: "John" }, { phone: "286456", name: "Mike" }, ] let result = array2.reduce((accumulator, currentItem) =>{ let index = accumulator.findIndex(item => item.name === currentItem.name); if(index === -1){ accumulator.push(currentItem); } return accumulator; },array1); console.log(result);

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

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