If you work with JavaScript you definitely have to deal with objects or array data structures. If you are not working with arrays and objects what could you be doing? Printing strings ha-ha.
How to check if array of objects has key with a particular value.
lets assume we have an array of objects with countries and id
const countries = [
{id:1,"country":"Sweden"},
{id:2,"country":"Ethiopia"},
{id:3,"country":"Canada"},
{id:5,"country":"India"}
]
Now we want to find out if we have a country with id 4; NOTE the key word is find;
Solution;
var result = countries.find(function(element) {
return element.id == 4;
});
The value of result can be either undefined if no value satisfied the condition on the return statement or the first value that satisfied the condition set in the return statement.