There is an array

var someArr = [ {name:"name1", prodid:1, prodinfo:true}, {name:"name2", prodid:2, prodinfo:false}, {name:"name3", prodid:3, prodinfo:false}, {name:"name4", prodid:4, prodinfo:true}, ]; 

There are id: 3 How to check the value of prodinfo for this id on true / false?

  • 3
    go through the array and find it? - teran
  • I didn't even think about it ..... - Maxim

2 answers 2

 function find(array, value) { for (var i = 0; i < array.length; i++) { if (array[i] == value) return i; } return i; } 

Something like this. I did not check the work, as from the phone. Creating an array search function, the first argument is an array, the second is the value we are looking for

It can be even simpler without a method: just loop through the array: for(var i = 0; i < arr.length;i++) {if arr[i] == false return arr[i];}

And if a specific id is even simpler, there is in the comparison loop of a specific id with the value of interest

     var someArr = [{ name: "name1", prodid: 1, prodinfo: true }, { name: "name2", prodid: 2, prodinfo: false }, { name: "name3", prodid: 3, prodinfo: false }, { name: "name4", prodid: 4, prodinfo: true }, ]; someArr.forEach(function(item) { if (item.prodid === 3) console.log(item.prodinfo) }); 

    • one
      Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky