get object from list of objects in vuejs with condition 1

get object from list of objects in vuejs with condition

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)

Here is what the above code is Doing:
1. It’s creating a data object with a specs array.
2. It’s creating a valObj variable and assigning it the result of the filter function.
3. The filter function is filtering the data.specs array and returning the element that has a Name property of “Power”.
4. If the valObj array has a length greater than 0, it’s logging the value of the Value property of the first element in the array.

Similar Posts