筛选具有多个条件的数组

时间:2019-03-28 08:00:26

标签: javascript arrays

数组看起来像

products = [
    {
      "id": "5c94aa7b7a37631ce2a97a72",
      "material": "5c9485d37804946bc487ce59",
    },
    {
      "id": "5c94aa7b7a37631ce2a97a72",
      "material": "5c94862a7804946bc487ce5e",
    },
    {
      "id": "5c94aa7b7a37631ce2a97a72",
      "material": "5c9486397804946bc487ce61",
    },
    {
      "id": "5c94a0a87a37631ce2a979de",
      "material": "5c9485d37804946bc487ce59",
    },
    {
      "id": "5c94a0a87a37631ce2a979de",
      "material": "5c94862a7804946bc487ce5e",
    },
    {
      "id": "5c94a0a87a37631ce2a979de",
      "material": "5c9486397804946bc487ce61",
    }
  ]

,当我尝试仅删除id="5c94aa7b7a37631ce2a97a72"material="5c9485d37804946bc487ce59"的一项

我使用过滤器功能

filtered = products.filter(x => x.id !== id && x.material !== material);

它将删除具有这些ID和材料的所有产品。在此功能中设置条件的正确方法是什么?

1 个答案:

答案 0 :(得分:5)

只需使用var markers = []; var infowindow = new google.maps.InfoWindow(); var map var pointerObj = [{ acLatitude: "34.005723", acLongitude: "-84.23386", name: "xyz", address: "abc", icon:"/img/as.jpg" }, { acLatitude: "34.005723", acLongitude: "-84.23386", name: "xyz", address: "abc", icon:"/img/as.jpg" }, { acLatitude: "34.005723", acLongitude: "-84.23386", name: "xyz", address: "abc", icon:"/img/as.jpg" }, { acLatitude: "34.005723", acLongitude: "-84.23386", name: "xyz", address: "abc", icon:"/img/as.jpg" } ]; function initMapObj() { map = new google.maps.Map(document.getElementById("googleMap"), { zoom: 16, center: new google.maps.LatLng(34.005813, -84.32682), mapTypeId: "roadmap", mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, position: google.maps.ControlPosition.TOP_RIGHT }, zoomControl: true, zoomControlOptions: { position: google.maps.ControlPosition.RIGHT_BOTTOM }, scaleControl: true, streetViewControl: true, streetViewControlOptions: { position: google.maps.ControlPosition.RIGHT_BOTTOM }, fullscreenControl: true }); } function bindMarkers() { var bounds = new google.maps.LatLngBounds(); for (var key in pointerObj) { var arrObj = pointerObj[key]; latLongCollection.push({ position: new google.maps.LatLng(arrObj.acLatitude, arrObj.acLongitude), name: arrObj.name, address: arrObj.address, }); } latLongCollection.forEach(function (pointerDtl) { var marker = new google.maps.Marker({ position: pointerDtl.position, icon: pointerDtl.icon, map: map }); markers.push(marker); bounds.extend(marker.position); //center the map respective of all the points google.maps.event.addListener(marker, "mouseover", (function (mm, tt) { //can be changed with 'click' event return function () { var infoContent = '<div class="infowindow">'; infoContent += '<div class="point-name">'+pointerDtl.catName+'</div>'; infoContent += '<div class="point-address">' + pointerDtl.address + '</div>'; infoContent += "</div>"; infowindow.setOptions({ content: infoContent }); infowindow.open(map, mm); }; })(marker, pointerDtl.address)); }); map.fitBounds(bounds) //center the map respective of all the points } 而不是||,那么两个条件都必须为false,以便&&回调返回filter

false