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


i have array like

vendors = [     {       name: 'magenic',       id: 'abc'      },     {       name: 'microsoft',       id: 'def'     } , on...  ]; 

how check array see if magenic exists? don't want loop, unless have to. i'm working potentially couple thousand records.

updated

since has been popular post, thought i'd share new found. , appears @cafxx has shared this! should read these more often. came across https://benfrain.com/understanding-native-javascript-array-methods/.

vendors.filter(function(vendor){ return vendor.name === "magenic" }); 

and ecmascript 2015 simpler using new arrow functions:

vendors.filter(vendor => (vendor.name === "magenic")); 

there no "magic" way check in array without loop. if use function, function use loop. can break out of loop find you're looking minimize computational time.

var found = false; for(var = 0; < vendors.length; i++) {     if (vendors[i].name == 'magenic') {         found = true;         break;     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -