Home How do I check if an array includes an object in JavaScript?
Reply: 0

How do I check if an array includes an object in JavaScript?

user607
1#
user607 Published in May 25, 2018, 11:11 am

What is the most concise and efficient way to find out if a JavaScript array contains an object?

This is the only way I know to do it:

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

Is there a better and more concise way to accomplish this?

This is very closely related to Stack Overflow question Best way to find an item in a JavaScript Array? which addresses finding objects in an array using indexOf.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.616413 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO