Skip to content
Fix Code Error

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

March 13, 2021 by Code Error
Posted By: Anonymous

I have an array like

vendors = [{
    Name: 'Magenic',
    ID: 'ABC'
  },
  {
    Name: 'Microsoft',
    ID: 'DEF'
  } // 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.

Solution

2018 edit: This answer is from 2011, before browsers had widely supported array filtering methods and arrow functions. Have a look at CAFxX’s answer.

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;
        break;
    }
}
Answered By: Anonymous

Related Articles

  • How to properly do JSON API GET requests and assign…
  • GLYPHICONS - bootstrap icon font hex value
  • Are 'Arrow Functions' and 'Functions' equivalent /…
  • pyspark window function from current row to a row…
  • What does this symbol mean in JavaScript?
  • Aurelia UX showcase app fails to load
  • Average values between two dates by group
  • Form field border-radius is not working only on the…
  • AppCompat v7 r21 returning error in values.xml?
  • Aurelia CLI viewport does not render
  • Best way to replace multiple characters in a string?
  • Piping df into mutate + substring expression
  • Why adding a criteria in for loop significantly…
  • Azure Availability Zone ARM Config
  • How to make a Vue plugin function call methods on…
  • Best way to compare two complex objects
  • Why does this Azure Resource Manager Template fail…
  • For-each over an array in JavaScript
  • Why is Polymer adding stuff to the (non-shadow) dom?
  • List of ANSI color escape sequences
  • Sort table rows In Bootstrap
  • How to Insert cell reference in VBA code for…
  • How to filter a RecyclerView with a SearchView
  • What's the meaning of "=>" (an arrow formed from…
  • What are the currently supported CSS selectors…
  • How to parse JSON with XE2 dbxJSON
  • Adding numbers and receiving a NaN
  • How does PHP 'foreach' actually work?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How not to get a repeated attribute of an object?
  • The 'compilation' argument must be an instance of…
  • How to compare arrays in JavaScript?
  • A fatal error has been detected by the Java Runtime…
  • Backbone JS, Marionette and Require JS
  • LINQ with groupby and count
  • Azure CLI - az deployment group create -…
  • Is this very likely to create a memory leak in Tomcat?
  • How can I wrap all BeautifulSoup existing…
  • How to convert number to words in java
  • Vue CLI 3 App: Uncaught SyntaxError on NPM Build
  • Detect if Visual C++ Redistributable for Visual…
  • How do I style a dropdown with only CSS?
  • Extract and display multiple strings in a single line
  • Maximum call stack size exceeded when binding…
  • Programmatically generate url from path and params
  • Combining pyOSC with pyQT5 / Threading?
  • Is this request generated by EF Core buggy or is it my code?
  • Avoid creating new session on each axios request laravel
  • What does do?
  • Changing date format in R
  • Equals implementation with override Equals,…
  • Error: the entity type requires a primary key
  • Is there some way to test my grammar which refer to…
  • How to set HTML5 required attribute in Javascript?
  • Why does C++ code for testing the Collatz conjecture…
  • How do I limit the number of digits from 6 to 4 in…
  • Cannot read property 'key' of undefined storybook react
  • How to pass multiple params in vue-router from…
  • Event Snippet for Google only shows one event while…
  • How to use java.Set
  • How is Pythons glob.glob ordered?
  • How do I include a JavaScript file in another…
  • Most effective way to parse JSON Objects
  • Search match multiple values in single field in…
  • Usage of __slots__?
  • Python is not calling fucntions properly
  • How does the "this" keyword work?
  • loop and eliminate unwanted lines with beautiful soup
  • Overriding the java equals() method - not working?
  • SQL Insert Query Using C#
  • Load and execution sequence of a web page?
  • How can I find the product GUID of an installed MSI setup?
  • C# parse FHIR bundle - read resources
  • ember-data: Loading hasMany association on demand
  • Compare 2nd and 3rd field of CSV file for each row…
  • Octave using 'for' statement to show two animations…
  • What are the real-world strengths and weaknesses of…
  • NullpointerException error while working with…
  • Firebase deployng Sapper app as cloud function failed
  • Arrow flip when dropdown opens JS
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Managing jQuery plugin dependency in webpack
  • What are the correct version numbers for C#?
  • Smart way to truncate long strings
  • How to use Servlets and Ajax?
  • What's the proper way to set a parent property in Backbone?
  • What does "Fatal error: Unexpectedly found nil while…
  • python pandas - add unique Ids in column from master…
  • vaadin-grid-filter for an Array of Strings not working
  • mongodb group values by multiple fields
  • How to handle Vue 2 memory usage for large data (~50…
  • What is an optional value in Swift?
  • How to turn off ALL conventions in Entity Framework Core 5
  • The result is not as intended, the output is a bound…
  • Is it possible to make abstract classes in Python?
  • Does moment.js allow me to derive a timezone…
  • Joining and comparing values of one df with first…
  • How to Make A Chevron Arrow Using CSS?
  • How to fix repeating data from SQL join?
  • How to traverse the complex nested Json in C# and…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

What is dependency injection?

Next Post:

Adding a background image to a element

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error