最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VueJS: Filter through objects in an array - Stack Overflow

programmeradmin0浏览0评论

I'm working on a vue-js problem.

I got an data-element (object) called items. I am looping through these products and display a dropdown with a list of the items.

Now I would like to show only those items who have a value in an array thats called "Watt" and a Title.

This is an example item of items:

Item

-Title

-Date

-Specifications [Array]

-- [0] Name: "Watt"

-- [0] Value: 5

-- [1] Name: "Weight"

-- [1] Value: 100

Any idea how to solve this?

I'm working on a vue-js problem.

I got an data-element (object) called items. I am looping through these products and display a dropdown with a list of the items.

Now I would like to show only those items who have a value in an array thats called "Watt" and a Title.

This is an example item of items:

Item

-Title

-Date

-Specifications [Array]

-- [0] Name: "Watt"

-- [0] Value: 5

-- [1] Name: "Weight"

-- [1] Value: 100

Any idea how to solve this?

Share Improve this question asked Feb 15, 2017 at 12:05 SPQRIncSPQRInc 2184 gold badges37 silver badges81 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

This is not VueJS specific. In Javascript you filter an array with Array#filter. Example:

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

To understand this have a look at Array#some and Array#filter functions. The above code basically filters the items array by the condition that the item has at least one (some) element in the Specification array where the Name is "Watt" and has a title.

发布评论

评论列表(0)

  1. 暂无评论