过滤对象以检查数组的所有元素是否存在

时间:2018-04-14 13:14:15

标签: javascript angular ecmascript-6 key-value

我是键值对概念的新手,并试图找出一种方法来过滤所有具有数组所有元素的键值对,但我在循环中的某个地方被击中。

我的键值对:

Alltrips = {
    "20180301": [{
            "transporter": {
                "company": "Pradeep Transport",
                "id": 1518419163555
            },
            "vehicleRegistrationNumber": "AS01FF1234",
            "destinations": [{
                "Id": "pcwYK",
                "v": "Bhubaneswar, OD"
            }, {
                "Id": "fy3yF",
                "v": "Tiruppur, TN"
            }],
            "Owner": "Gogs"
        },
        {
            "transporter": {
                "company": "tes Transport",
                "id": 1518419163515
            },
            "vehicleRegistrationNumber": "AS01FF1454",
            "destinations": [{
                "Id": "coWoz",
                "v": "Vizag, AP"
            }, {
                "Id": "EZuh7",
                v: "Ambala Sadar, HR"
            }],
            "Owner": "Acme"
        }
    ],
    "20180406": [{
            "transporter": {
                "company": "tes Transport",
                "id": 15184195663555
            },
            "vehicleRegistrationNumber": "AS01FF1444",
            "destinations": [{
                "Id": "pcqYK",
                "v": "Coorg"
            }, {
                "Id": "fy3yF",
                "v": "Tiruppur, TN"
            }],
            "Owner": "pyu"
        }

    ],
    "20180412": [{
            "transporter": {
                "company": "tes Transport",
                "id": 15184195663555
            },
            "vehicleRegistrationNumber": "AS01FF1144",
            "destinations": [{
                "Id": "pcqYK",
                "v": "Coorg"
            }, {
                "Id": "fy3yF",
                "v": "Tiruppur, TN"
            }],
            "Owner": "ps"
        }

    ]


}

我有一个数组:

searchOptions= 
[{label: "tes cargo", value: "tes cargo", type: "TransporterType"}, {label: "AS01FF1444", value: "AS01FF1444", type: "VehicleType"},{label: "Coorg State, Kodagu Dist., Karnataka", value: "Coorg State, KA", type: "PlaceType"}]

我必须返回一个结果对象" final "其中包含所有" Alltrips "包含 searchOptions.value

的键值对

例如对于给定的数组" searchOptions"以上,我的结果" final"对象应该是:

final= {
"20180406": [{
            "transporter": {
                "company": "tes Transport",
                "id": 15184195663555
            },
            "vehicleRegistrationNumber": "AS01FF1444",
            "destinations": [{
                "Id": "pcqYK",
                "v": "Coorg"
            }, {
                "Id": "fy3yF",
                "v": "Tiruppur, TN"
            }],
            "Owner": "pyu"
        }

    ]

}

我尝试过的解决方案是:

var final={};
    var temp=[];
    for(var dates in this.alltrips) {
      if(dates>=mindate && dates <=maxdate) {
        for(var i=0;i<this.alltrips[dates].length; i++) {
          //for(let options in this.alltrips[dates][i]) {

            for(var j=0;j<searchOptions.length;j++) {

              if(searchOptions[j].type== "TransporterType") {
                var flag=true;
                if(searchOptions[j].value == this.alltrips[dates][i].transporter.company)
                  flag=true
                else {
                  flag=false
                }
              }
              else if(searchOptions[j].type== "VehicleType"){
                if(searchOptions[j].value == this.alltrips[dates][i].vehicleRegistrationNumber)
                  flag=true
                else 
                  flag=false
              }
              if(searchOptions[j].type== "PlaceType") {
                //if(this.alltrips[dates][i]== 'destinations') {
                  for(var k=0;k<this.alltrips[dates][i].destinations.length;k++){
                    if(searchOptions[j].value== this.alltrips[dates][i].destinations[k].v)
                      flag=true
                    else
                      flag=false
                  }
               // }
              }
              if(flag==true) {
                temp.push(this.alltrips[dates][i])

              }
              else 
                break;
                final[dates]= temp;
                temp=[]
            }
          //}

        }
      }
    }
    console.log(temp)
    console.log(final)

上面代码的问题是它只返回每个键的单个值,如:

{"20180406" :  Array(1)}

如果每个键有多个匹配,则只返回一个。例如,我可能会有一些匹配结果可能是:

{"20180406" : Array(2), "20180412" : Array(2)}

但是,对于任何匹配,它只返回一个数组匹配。我该如何解决?

编辑:我找到了答案并更新了主要功能。使用&#34;继续&#34;做了诀窍

var final = {};
      var temp = [];
      var flag=false;
      for (var dates in this.alltrips) {
        temp=[]
        if(dates >= mindate && dates <= maxdate) {
          loop1 : for (var i = 0; i < this.alltrips[dates].length; i++) {
            for(var j = 0; j < samplearray.length; j++) {
              if (samplearray[j].type == "VehicleType") {
                if (samplearray[j].value == this.alltrips[dates][i].vehicleRegistrationNumber) {
                flag=true;
                }
                else {
                  flag=false;
                  continue loop1;
                }
              } else if (samplearray[j].type == "TransporterType") {
                  if (samplearray[j].value == this.alltrips[dates][i].transporter.company) {
                    flag=true;
                  }
                  else{
                    flag= false;
                    continue loop1;
                  }
              } else if(samplearray[j].type == "PlaceType") {
                  for(var k=0;k< this.alltrips[dates][i].destinations.length;k++) {
                    if (samplearray[j].value == this.alltrips[dates][i].destinations[k].v) {
                      flag=true;
                    }
                    else {
                      flag= false;
                      continue loop1;
                    }
                  }
              } else if (samplearray[j].type == "OwnerType") {
                  if (samplearray[j].id == this.alltrips[dates][i].operatorId) {
                    flag=true;
                  }
                  else{
                    flag= false;
                    continue loop1;
                  }
               } 
            }
            if(flag==true) {
            temp.push(this.alltrips[dates][i])
            final[dates]= temp;
            }
          }
        }
      }
     console.log(final)

1 个答案:

答案 0 :(得分:0)

这很好用:

<html>

  <body>
    <div id="result"></div>
  </body>
  <script>
    Alltrips = {
      "20180301": [{
          "transporter": {
            "company": "Pradeep Transport",
            "id": 1518419163555
          },
          "vehicleRegistrationNumber": "AS01FF1234",
          "destinations": [{
            "Id": "pcwYK",
            "v": "Bhubaneswar, OD"
          }, {
            "Id": "fy3yF",
            "v": "Tiruppur, TN"
          }],
          "Owner": "Gogs"
        },
        {
          "transporter": {
            "company": "tes Transport",
            "id": 1518419163515
          },
          "vehicleRegistrationNumber": "AS01FF1454",
          "destinations": [{
            "Id": "coWoz",
            "v": "Vizag, AP"
          }, {
            "Id": "EZuh7",
            v: "Ambala Sadar, HR"
          }],
          "Owner": "Acme"
        }
      ],
      "20180406": [{
          "transporter": {
            "company": "tes Transport",
            "id": 15184195663555
          },
          "vehicleRegistrationNumber": "AS01FF1444",
          "destinations": [{
            "Id": "pcqYK",
            "v": "Coorg"
          }, {
            "Id": "fy3yF",
            "v": "Tiruppur, TN"
          }],
          "Owner": "pyu"
        }

      ],
      "20180412": [{
        "transporter": {
          "company": "tes Transport",
          "id": 15184195663555
        },
        "vehicleRegistrationNumber": "AS01FF1444",
        "destinations": [{
          "Id": "pcqYK",
          "v": "Coorg"
        }, {
          "Id": "fy3yF",
          "v": "Tiruppur, TN"
        }],
        "Owner": "ps"
      }]
    };

    searchOptions = [
      {
              label: "tes cargo",
              value: "tes cargo",
              type: "TransporterType"
            },
      {
        label: "AS01FF1444",
        value: "AS01FF1444",
        type: "VehicleType"
      },
      {
        label: "Coorg State, Kodagu Dist., Karnataka",
        value: "Coorg State, KA",
        type: "PlaceType"
      }
    ];
    window.onload = function() {
      var resultDiv = document.getElementById('result');
      var final = {};
      var temp = [];
      for (var dates in Alltrips) {
        for (var i = 0; i < Alltrips[dates].length; i++) {
          for (var j = 0; j < searchOptions.length; j++) {
            console.log(searchOptions[j]);
            console.log(Alltrips[dates][i]);
            if (searchOptions[j].type == "TransporterType") {
              var flag = true;
              if (searchOptions[j].value == Alltrips[dates][i].transporter.company) {
                console.log("found")
                flag = true
              } else
                flag = false
            } else if (searchOptions[j].type == "VehicleType") {
              if (searchOptions[j].value == Alltrips[dates][i].vehicleRegistrationNumber) {
                console.log("found")
                flag = true
              } else
                flag = false
            } else if (searchOptions[j].type == "PlaceType") {
              for (var k = 0; k < Alltrips[dates][i].destinations.length; k++) {
                if (searchOptions[j].value == Alltrips[dates][i].destinations[k].v) {
                  console.log("found")
                  flag = true
                } else
                  flag = false
              }
            }
            if (flag == true) {
              temp.push(Alltrips[dates][i])
              final[dates] = temp;
            }
            console.log(dates);
            temp = []
          }
        }
      }
      console.log(temp)
      console.log(final)
      document.getElementById("result").innerHTML = JSON.stringify(final);
    };

  </script>

</html> 

当然它并不匹配任何东西,但是如果你更改搜索过滤器以使其与Alltrips中的某些内容匹配则可以。 https://jsfiddle.net/zs8xmaLx/35/