【问题标题】:How to check if in an array of objects two equal properties are repeated at the same time?如何检查对象数组中两个相等的属性是否同时重复?
【发布时间】:2020-04-27 15:34:08
【问题描述】:

我有以下对象数组:

[
 { 
   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"   
 },
]

我需要检查哪些对象同时重复值“homeTerminal”和“destinationTerminal”。正如我们在示例中看到的,我需要同时检索两个属性中具有相同值的第一个和第三个对象。

我尝试制作地图,但我只能使用一个值来完成,而且我不知道如何同时检查两个值。 提前谢谢你。

【问题讨论】:

    标签: javascript arrays filter arrayobject


    【解决方案1】:

    你可以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方法有2个参数:

    1. 为数组的每个元素执行的回调函数。
    2. 返回值的初始值
      1. 此值必须从回调函数返回

    回调函数依次接受 3 个参数:

    1. return_value (retArr)
    2. 数组元素 (item)
    3. 索引未使用

    可以对数组元素进行任何操作,结果必须反映在 return_value 中(此处为展开值的接触操作)

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

    【讨论】:

      【解决方案2】:
      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]);
                  }
              }
          });
      

      应该可以。你在 r 数组中得到结果

      【讨论】:

        【解决方案3】:

        您可以使用由字段 homeTerminaldestinationTerminal 组成的复合键来计算查找/映射。

        然后使用该查找来查找重复元素。

        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>

        【讨论】:

        • 非常感谢您的这一观点。我不熟悉,所以我会尝试一下
        猜你喜欢
        • 2021-08-19
        • 1970-01-01
        • 1970-01-01
        • 2012-06-10
        • 1970-01-01
        • 2017-02-20
        • 2012-12-31
        • 2019-08-24
        相关资源
        最近更新 更多