嵌套数组过滤

时间:2020-05-27 19:31:42

标签: javascript arrays

categories.location数组的过滤方式如下:

  const displayedCategories = categories.filter(
    (category) => category.location.indexOf(selectedLocation) >= 0
  );

现在,其结构如下所示进行了更改,还有其他嵌套值。现在如何过滤值/标签? category.location.value.indexOf不起作用。谢谢。

"location" : [ 
    {
        "value" : "London",
        "label" : "London"
    }
             ]

1 个答案:

答案 0 :(得分:1)

尝试:

const displayedCategories = categories.filter(
    (category) => category.location.some(loc=>loc.value==selectedLocation)
  );