SPQRInc SPQRInc - 8 months ago 46
Javascript Question

JavaScript/VueJS: Check if an Array contains an object with an element that has specific value

I would like to solve this problem:

I got an Object that contains a property named

. This property contains an
that all have 2 properties:

  • name

  • value

So my object is like this:

-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?

Answer Source

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"},{

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

if(valObj.length > 0)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download