So my question how can I check this array of objects by "key" for eg I want to check if the key 3892 is there, I have tried with indexOf but no luck, I can't use a loop.
2 Answers
You can use some() and hasOwnProperty()
var array = [{3892: 'value'}, {1234: 'value'}];
var check = array.some(obj => obj.hasOwnProperty(3892));
console.log(check)
4 Comments
Uffo
Do you also now how can I get the value based on this? because this returns true which is good but I also need the value
Nenad Vracar
Do you mean value as object?
Uffo
Well let's say in this case for 3892 I need the value.. in this case 'value'
Nenad Vracar
You can return object using
find and then check if object is returned and access value jsfiddle.net/Lg0wyt9u/1774You can chain Object.keys with Array.prototype.includes to achieve that
Object.keys(myObject).includes(myKey);
const myObject = { name: 'Peter' };
const myKey = 'name';
const result = Object.keys(myObject).includes(myKey);
console.log(`Includes key ${myKey}? `, result);

Object.keys(yourJSONObject)first to get array of key and search in that array developer.mozilla.org/en/docs/Web/JavaScript/Reference/…