8

I want to check if a value exist in an array object. Example:

I have this array:

[
    {id: 1, name: 'foo'},
    {id: 2, name: 'bar'},
    {id: 3, name: 'test'}
]

And I want check if id = 2 exists here.

0

6 Answers 6

24

You can use Array.prototype.some

var a = [
   {id: 1, name: 'foo'},
   {id: 2, name: 'bar'},
   {id: 3, name: 'test'}
];        

var isPresent = a.some(function(el){ return el.id === 2});
console.log(isPresent);

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

3 Comments

it works! but with this change: var isPresent = a.some((el) => { el.id === 2});
yes sure, i kept es5 syntax just for the example :)
@stiv You have to remove the curly braces or it will return an object instead of a falsy value: var isPresent = a.some((el) => el.id === 2);
7

You can use some().

If you want to just check whether a certain value exists or not, the Array.some() method (since JavaScript 1.6) is fair enough as already mentioned.

let a = [
   {id: 1, name: 'foo'},
   {id: 2, name: 'bar'},
   {id: 3, name: 'test'}
];        

let isPresent = a.some(function(el){ return el.id === 2});
console.log(isPresent);

Also, find() is a possible choice.

If you want to fetch the entire very first object whose certain key has a specific value, it is better to use the Array.find() method which has been present since ES6.

let hasPresentOn = a.find(
  function(el) {
  return el.id === 2
  }
);
console.log(hasPresentOn);

Comments

6

You can use the find method as below:

var x=[
    {id: 1, name: 'foo'},
    {id: 2, name: 'bar'},
    {id: 3, name: 'test'}
]
var target=x.find(temp=>temp.id==2)
if(target)
  console.log(target)
else
  console.log("doesn't exists")

Comments

1

Try this:

let idx = array.findIndex(elem => elem.id === 2)
if (idx !== -1){
    // Your element exists
}

Comments

1
var isExist = a.some((el)=>{ return el.id === 2});
console.log(isExist);

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
0

You could traverse recursively your collection and then you can find the match no matter how deeply nested it is. Beware cycles though. If there might be cycles in the input, then you will need extra measures.

function exists(collection, pattern) {
    for (let item in collection) {
        if ((item === pattern.key) && (collection[item] === pattern.value)) return true;
        if (Array.isArray(collection[item]) || (typeof collection[item] === "object") && exists(collection[item], pattern)) return true;
    }
    return false;
}

console.log(exists([
    {},
    {},
    {abc: "def"}
], {key: "abc", value: "def"}));
console.log(exists([
    {},
    {},
    {abc: "def"}
], {key: "abc", value: "degf"}));
console.log(exists([
    {},
    {},
    {abc: "def"}
], {key: "abcd", value: "def"}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.