[英]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 .您可以使用由字段homeTerminal和destinationTerminal组成的复合键来计算查找/映射。
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:
callback function in turn takes 3 arguments:回调 function 依次占用 3 个 arguments:
retArr
) retArr
)item
)item
)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.