使用多个选项过滤JSON

时间:2018-11-14 16:44:19

标签: javascript ionic3

我有一个JSON:

location_filter = [{ title: "My Library", type: "library"},
      { title: "My Parking", type: "parking" },
      { title: "My Info", type: "info" }]

我有一个包含三个选项的“多重”下拉列表:“信息”,“停车”,“图书馆”。

我已经发送了要过滤的值,该值可以是一个或多个。

我知道如何只使用一项过滤器。

我的问题是:如何与多个物品一起使用?

splitFilter(array){
    var arr = array.split(',');
    return arr;
  }


filter(selection) {

    let stringSplit = this.splitFilter(selection.toString());

        this.location_filter = JSON.parse(JSON.stringify(this.locations));

        this.location_filter = this.location_filter.filter((item) => {
            return item.type === selection;
        });

    this.loadMap(this.location_filter);
}

我已经在使用splitFilter()函数分割字符串了

也许是循环?

1 个答案:

答案 0 :(得分:2)

您可以使用Array#includes进行确认。

this.location_filter = this.location_filter.filter(({ type }) => selection.includes(type));

或者与ES5一起使用Array#indexOf并检查是否-1找不到物品。

this.location_filter = this.location_filter.filter((item) => {
    return selection.indexOf(item.type) !== -1;
});
相关问题