Lodash多个键具有多个值数组过滤器

时间:2018-01-06 10:48:23

标签: javascript jquery vue.js lodash

我正在尝试使用lodash过滤器对象过滤多维对象。下面是我过滤数据的示例对象。

    {
  "0": {
    "details": {
      "rating": 2.5,
      "amenities": {
        "airConditioning": true,
      },
    },
    "rates": {
      "packages": [
        {
          "refundable": "Yes",
        }
      ]
    }
  },
  {
  "1": {
    "details": {
      "rating": 3,
      "amenities": {
        "airConditioning": false,
      },
    },
    "rates": {
      "packages": [
        {
          "refundable": "Yes",
        }
      ]
    }
  },
  {
  "2": {
    "details": {
      "rating": 2,
      "amenities": {
        "airConditioning": true,
      },
    },
    "rates": {
      "packages": [
        {
          "refundable": "No",
        }
      ]
    }
  },
}

我试过了

    console.log(_.filter( data, { 'details.rating': '2', 'details.rating': '3' } ));

_.filter(data, function (item) {
  return ['2', '3'].indexOf(item.details.rating) >= 0
})

并遵循此Filtering multiple value with multiple key in json array using lodash

无效工作

1 个答案:

答案 0 :(得分:1)

您的JSON在密钥List<WebElement> Add_Tenant_Info = driver.findElements(By.xpath("//table[@class='table_view']//tbody//tr[@class='false sub_row data_row' or @class='false sub_row data_row highlight']//a[contains(.,'Add Tenant Info')]")); for(int i=0; i<Add_Tenant_Info.size();i++) { //Click on any Add Tenant Info link through index "i" Add_Tenant_Info.get(i).click(); } {之前有额外的"1"。所以删除它,它会工作。

&#13;
&#13;
"2"
&#13;
    var data = {
      "0": {
        "details": {
          "rating": 2.5,
          "amenities": {
            "airConditioning": true,
          },
        },
        "rates": {
          "packages": [
            {
              "refundable": "Yes",
            }
          ]
        }
      },
      
      "1": {
        "details": {
          "rating": 3,
          "amenities": {
            "airConditioning": false,
          },
        },
        "rates": {
          "packages": [
            {
              "refundable": "Yes",
            }
          ]
        }
      },
      "2": {
        "details": {
          "rating": 2,
          "amenities": {
            "airConditioning": true,
          },
        },
        "rates": {
          "packages": [
            {
              "refundable": "No",
            }
          ]
        }
      }
    };
    var result = _.filter(data, (item) => {
      return [2, 3].indexOf(item.details.rating) >= 0
    });
    console.log(result);
&#13;
&#13;
&#13;

注意过滤器<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>内的数组元素。它们是整数类型,因为[2, 3]是整数。