check if dictionary object in array contains certain value in javascript

javascript

Solution

To check if object exists in array you can use `some()` that returns `true/false`

const myArray = [{key1: "value1", key2: "value2"}, {key1: "value4", key2: "value8"}, {key1: "value1", key2: "value3"}, {key1: "value1", key2: "value32"}]
  
if(myArray.some(e => e.key1 == 'value1')) {
  console.log('Exists');
}

Problem

I have an array, like so: ``` const myArray = [{key1: "value1", key2: "value2"}, {key1: "value4", key2: "value8"}, {key1: "value1", key2: "value3"}, {key1: "value1", key2: "value32"}] ``` I need to loop through the array, and see if ANY item in the array has a certain value. I have looked through other questions and most loop through and create variables, however I am not allowed to use var (ES6 something, I'm a js noob and am not sure, just was told to never use var, always const). Basically I need something like this: ``` if (myArray.contains(object where key1=="value2")) { // do something } ``` I would like for the function to return true/false as well, not the object itself. thanks

Original source

Related problems