3

enter image description here

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.

1

2 Answers 2

8

You can use some() and hasOwnProperty()

var array = [{3892: 'value'}, {1234: 'value'}];

var check = array.some(obj => obj.hasOwnProperty(3892));
console.log(check)

Sign up to request clarification or add additional context in comments.

4 Comments

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
Do you mean value as object?
Well let's say in this case for 3892 I need the value.. in this case 'value'
You can return object using find and then check if object is returned and access value jsfiddle.net/Lg0wyt9u/1774
2

You 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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.