Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
41
rated 0 times [  48] [ 7]  / answers: 1 / hits: 64188  / 7 Years ago, fri, february 3, 2017, 12:00:00

I would like to solve this problem:



I got an Object that contains a property named specs. This property contains an Array of Objects that all have 2 properties:




  • name

  • value



So my object is like this:



Object
-Title
-Date
-Specs [Array]
-- [0] Name: Power
-- [0] Value: 5
-- [1] Name: Weight
-- [1] Value: 100


So - now I would like to check, if my Specs-Array contains an item that has the name Power. If this is the case, I would like to use the value of this element.



How can I solve this?


More From » vue.js

 Answers
10

You can filter the array based on the name attribute and check if the filter returns a result array , if yes then you can use it with index to get the value.





var data = {specs:[{Name:Power,Value:1},{
Name:Weight,Value:2},{Name:Height,Value:3}]}

var valObj = data.specs.filter(function(elem){
if(elem.Name == Power) return elem.Value;
});

if(valObj.length > 0)
console.log(valObj[0].Value)




[#59085] Wednesday, February 1, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
addie

Total Points: 699
Total Questions: 89
Total Answers: 97

Location: Cayman Islands
Member since Fri, Mar 4, 2022
2 Years ago
;