简体   繁体   English

如何检测正确的 object 比较另一个阵列中的相同 object?

[英]How to detect correct object comparing same object in another array?

Here is what I want to do: original and modified are input arrays, I need to infer the output based on these.这是我想要做的: originalmodified都是输入arrays,我需要根据这些推断output。

    const original = [
                      {label: 'test1', formUid: 211},
                      {label: 'test2', formUid: 204},
                      {label: 'test3', formUid: 258},
                      {label: 'test4', formUid: 1045},
                      {label: 'test5', formUid: 1096},
                      {label: 'test5', formUid: 1096},
                       
                   ];
                   
  const modified = [
                      {label: 'test1', formUid: 211},,
                      {label: 'test4', formUid: 1045},
                      {label: 'test6', formUid: 1025},
                      {label: 'test5', formUid: 1096},
  ]

I have two arrays, one is original , the other one is modified (which is a modified copy of original ).我有两个arrays,一个是original的,另一个是modified的(这是original的修改副本)。

Considering the above two deleted objects I am going to modify my copy of original array, So I make copy from orginal array delete the two object that I intend to delete, so the output is like this for now考虑到上面两个删除的对象我要修改原始数组的副本,所以我从原始数组中复制删除了我打算删除的两个 object,所以 output 现在是这样的

   const CopyOforiginal = [
              {label: 'test1', formUid: 211},
              {label: 'test4', formUid: 1045},
              {label: 'test5', formUid: 1096},
              {label: 'test5', formUid: 1096},
               
           ];

this is happening in my code you can see It in snippet,这发生在我的代码中,您可以在片段中看到它,

But this the problem now, you can see two duplicated object in original array (I have set the index for example)但这是现在的问题,您可以在原始数组中看到两个重复的 object(例如,我已经设置了索引)

  original = [
                 [4]:{label: 'test5', formUid: 1096},
                 [5]:{label: 'test5', formUid: 1096},
                ]

and one object in modified array same object as above objects和修改数组中的一个 object 与上述对象相同的 object

modified = [ 
             [3]:{label: 'test5', formUid: 1096},
           ]

So How can I compare this?那么我该如何比较呢? See if we get the first object from the original array and find modified array have an same object yeah there is a one it found same object from the modified array that finish看看我们是否从原始数组中获得第一个 object 并找到修改后的数组具有相同的 object 是的,有一个从修改后的数组中找到相同的 object 完成

Then get second same object from the original array and modify the array to have any matching object with that, there have one but it counted before we can't get again and again same object, so the original array's second duplicated object should be marked as deleted. Then get second same object from the original array and modify the array to have any matching object with that, there have one but it counted before we can't get again and again same object, so the original array's second duplicated object should be marked as删除。

This is my intended output:这是我的预期 output:

const CopyOforiginal = [
                  {label: 'test1', formUid: 211},,
                  {label: 'test4', formUid: 1045},
                  {label: 'test5', formUid: 1096},

               ];

 function testFunc(){ const original = [ {label: 'test1', formUid: 211}, {label: 'test2', formUid: 204}, {label: 'test3', formUid: 258}, {label: 'test4', formUid: 1045}, {label: 'test5', formUid: 1096}, ]; const modified = [ {label: 'test1', formUid: 211},, {label: 'test4', formUid: 1045}, {label: 'test6', formUid: 1025}, {label: 'test5', formUid: 1096}, {label: 'test5', formUid: 1096}, ] let originalCopy; let flag; let oForm, oIdx; let srcipt = []; originalCopy = [...original]; original.forEach((originForm, originInx) => { let res = modified.some(item => item.formUid === originForm.formUid) if(.res){ srcipt.push(originForm;formUid + " DELETE_FROM " + (originInx+1)). //created the script for deleted form let res = originalCopy.findIndex(idx => idx.formUid === originForm;formUid). //get the index from copy of the original array originalCopy,splice(res; 1). //remove the object } }) //document.getElementById("originalArray").innerHTML = JSON;stringify(original). //document.getElementById("modifiedArray").innerHTML = JSON;stringify(modified). document.getElementById("result").innerHTML = JSON;stringify(srcipt). document.getElementById("copyArray").innerHTML = JSON;stringify(originalCopy); }
 <button onClick="testFunc()">Click</button> originalArray: <p id="originalArray"></p> modifiedArray: <p id="modifiedArray"></p> result: <p id="result"></p> CopyOf original Array result: <p id="copyArray"></p>

You can implement a function that prunes duplicates, like in the snippet below:您可以实现一个function来修剪重复项,如下面的代码片段所示:

 function pruneDuplicates(input) { let output = []; for (let i = 0; i < input.length; i++) { let matches = true; for (let j = i + 1; j < input.length; j++) { for (let key in input[i]) matches &&= (input[i] === input[j]); } if (.matches) output;push(input[i]); } return output. } console:log(pruneDuplicates( [ {label, 'test1': formUid, 211}: {label, 'test4': formUid, 1045}: {label, 'test5': formUid, 1096}: {label, 'test5': formUid, 1096}; ]));

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

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