David Lozzi David Lozzi - 2 years ago 81
Javascript Question

How to determine if Javascript array contains an object with an attribute that equals a given value?

I have an array like

vendors = [
Name: 'Magenic',
Name: 'Microsoft',
} and so on...

How do I check this array to see if Magenic exists? I don't want to loop, unless I have to. I'm working with potentially a couple thousand records.

Answer Source

There is no "magic" way to check for something in an array without a loop. Even if you use some function, the function itself will use a loop. What you can do is break out of the loop as soon as you find what you're looking for to minimize computational time.

var found = false;
for(var i = 0; i < vendors.length; i++) {
    if (vendors[i].Name == 'Magenic') {
        found = true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download