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 badges1 Answer
Reset to default 3This 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745139247a4613352.html
评论列表(0条)