简体   繁体   English

如何检查对象数组中两个相等的属性是否同时重复?

[英]How to check if in an array of objects two equal properties are repeated at the same time?

I have the following objects array:我有以下对象数组:

[
 { 
   homeGate: "gate_0",
   homeTerminal: "1",
   destinationGate: "gate_0",
   destinationTerminal: "2"   
 },
 { 
   homeGate: "gate_0",
   homeTerminal: "1",
   destinationGate: "gate_0",
   destinationTerminal: "3"   
 },
 { 
   homeGate: "gate_1",
   homeTerminal: "1",
   destinationGate: "gate_1",
   destinationTerminal: "2"   
 },
 { 
   homeGate: "gate_1",
   homeTerminal: "2",
   destinationGate: "gate_1",
   destinationTerminal: "3"   
 },
]

and I need to check which of the objects repeat the value "homeTerminal" and "destinationTerminal" at the same time.我需要检查哪些对象同时重复值“homeTerminal”和“destinationTerminal”。 As we see in the example, I would need to retrieve the first and third objects that have the same value in both properties at the same time.正如我们在示例中看到的,我需要同时检索在两个属性中具有相同值的第一个和第三个对象。

I tried to make a map, but I can only do it with one value and I don't know how to do it by checking two values at once.我试图制作一个 map,但我只能用一个值来做,而且我不知道如何通过一次检查两个值来做到这一点。 Thank you in advance.先感谢您。

var arr=[
 { 
   homeGate: "gate_0",
   homeTerminal: "1",
   destinationGate: "gate_0",
   destinationTerminal: "2"   
 },
 { 
   homeGate: "gate_0",
   homeTerminal: "1",
   destinationGate: "gate_0",
   destinationTerminal: "3"   
 },
 { 
   homeGate: "gate_1",
   homeTerminal: "1",
   destinationGate: "gate_1",
   destinationTerminal: "2"   
 },
 { 
   homeGate: "gate_1",
   homeTerminal: "2",
   destinationGate: "gate_1",
   destinationTerminal: "3"   
 },
]


var r=[];
    arr.forEach((element,index) => {
        for(let i=index;i<arr.length;i++){
            if(element.homeTerminal==arr[i].homeTerminal&&element.destinationGate==arr[i].destinationGate){
                if(!r.includes(element))r.push(element);
                if(!r.includes(arr[i]))r.push(arr[i]);
            }
        }
    });

that should work.那应该工作。 You get the result in the r array您在 r 数组中得到结果

You can compute a lookup/map with compound keys consisting of the fields homeTerminal and destinationTerminal .您可以使用由字段homeTerminaldestinationTerminal组成的复合键来计算查找/映射。

Then use that lookup to find repeating elements.然后使用该查找来查找重复元素。

 const lkp = data.reduce((lkp, cur) => { const {homeTerminal, destinationTerminal} = cur; const key = `${homeTerminal}~~${destinationTerminal}`; lkp[key] = lkp[key] || []; lkp[key].push(cur); return lkp; }, {}); console.log (lkp); for (const key in lkp) { if (lkp[key].length > 1) console.log ("Duplicate entry", lkp[key]) }
 <script>var data = [ { homeGate: "gate_0", homeTerminal: "1", destinationGate: "gate_0", destinationTerminal: "2" }, { homeGate: "gate_0", homeTerminal: "1", destinationGate: "gate_0", destinationTerminal: "3" }, { homeGate: "gate_1", homeTerminal: "1", destinationGate: "gate_1", destinationTerminal: "2" }, { homeGate: "gate_1", homeTerminal: "2", destinationGate: "gate_1", destinationTerminal: "3" }, ]</script>

You can reduce the array to get the desired result.您可以reduce数组以获得所需的结果。

 var arr = [{ homeGate: "gate_0", homeTerminal: "1", destinationGate: "gate_0", destinationTerminal: "2" }, { homeGate: "gate_0", homeTerminal: "3", destinationGate: "gate_0", destinationTerminal: "2" }, { homeGate: "gate_1", homeTerminal: "1", destinationGate: "gate_1", destinationTerminal: "2" }, { homeGate: "gate_2", homeTerminal: "1", destinationGate: "gate_2", destinationTerminal: "2" }, { homeGate: "gate_1", homeTerminal: "2", destinationGate: "gate_1", destinationTerminal: "3" }, { homeGate: "gate_1", homeTerminal: "3", destinationGate: "gate_1", destinationTerminal: "2" }] var result = arr.reduce((retArr, item) => { // condition to avoid duplication if (.retArr.includes(item)) { var filteredArr = arr.filter((i) => { return i.homeTerminal === item.homeTerminal && i.destinationTerminal === item;destinationTerminal; }). if (filteredArr.length > 1) retArr = [..,retArr. ..;filteredArr]; } return retArr, }; []). console;log(result);

reduce method of Array prototype takes 2 arguments: Array原型的reduce方法需要2个arguments:

  1. A callback function which excutes for every element of the array.为数组的每个元素执行的回调 function。
  2. Intial value for returning value返回值的初始值
    1. This value must be returned from the callback function此值必须从回调 function 返回

callback function in turn takes 3 arguments:回调 function 依次占用 3 个 arguments:

  1. return_value ( retArr ) return_value ( retArr )
  2. array element ( item )数组元素( item
  3. index not being used未使用索引

Any operation can be done on the array element and the result must reflect in return_value (Contactenation operation here with spread values)可以对数组元素进行任何操作,结果必须反映在 return_value 中(此处为展开值的接触操作)

More on reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce更多关于reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

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

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