【问题标题】:VueJS: Filter through objects in an arrayVueJS:过滤数组中的对象
【发布时间】:2017-02-15 12:05:31
【问题描述】:

我正在处理一个 vue-js 问题。

我有一个名为itemsdata 元素(对象)。我正在循环浏览这些产品并显示一个带有项目列表的下拉列表。

现在我想仅显示那些在名为“Watt”和Title 的数组中具有值的项目。

这是items的示例项目:

Item

-Title

-Date

-Specifications [Array]

-- [0] Name: "Watt"

-- [0] Value: 5

-- [1] Name: "Weight"

-- [1] Value: 100

知道如何解决这个问题吗?

【问题讨论】:

    标签: javascript arrays vuejs2 vue.js


    【解决方案1】:

    这不是 VueJS 特定的。在 Javascript 中,您使用 Array#filter 过滤数组。示例:

    items = items.filter(function(item) {
        return item.Title && item.Specifications.some(function(specification) {
             return specification.Name === "Watt";
        });    
    });
    

    要了解这一点,请查看 Array#someArray#filter 函数。上面的代码基本上过滤了 items 数组,条件是 item 在 Specification 数组中至少有一个(一些)元素,其中 Name 是“Watt”并且有一个标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 2020-02-07
      相关资源
      最近更新 更多