You may have seen yourself in this situation when coding in JavaScript: you have an array of objects, and you need to find some specific object inside this array based on some property of the object. There are A LOT of ways of achieving this, so I decided to put some of them together in this post to make it easy for you to pick up whatever works best for you. Let's check them out!

Javascript Generate Dynamic Key Values In An Object

Looping through objects in JavaScript 20th Jun 2018. Once in a while, you may need to loop through Objects in JavaScript. The only way to do so before ES6 is with a for.in loop. The problem with a for.in loop is that it iterates through properties in the Prototype chain. When you loop through an object with the for.in loop, you need to check if the property belongs to the object.

First, let's say that we have this array of objects called 'objArray':

And let’s say that we want to get the object that has the id equal to 3, let’s see how we can achieve that.

Using Vanilla JS (aka plain old JavaScript)

If you want to use the plain old javascript you can create a function that iterates over the array and then return the object when the id matches with the one you’re looking for (or return null if it doesn’t find a matching result):

Then you can pass the array you want to lookup, the property you’re looking for and its value. In our case we would use it like this:

  1. Nov 12, 2019  Object.entries The Object.entries method was also introduced with ES2017, and the method returns an array of a given object’s own enumerable string-keyed property key, value pairs, in.
  2. Mar 20, 2017  JavaScript: find an object in array based on object's property (and learn about the 'find' function). If you want to use the plain old javascript you can create a function that iterates over.
  3. Object.keys(obj) Parameters obj The object of which the enumerable's own properties are to be returned. Return value. An array of strings that represent all the enumerable properties of the given object. Object.keys returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given.
  4. Object.values returns an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by looping over the property values of the object.

Using ES6

If you prefer using the newer ES6 functions, you can also use the built-in find function, so we can achieve the same result without the need to implement our own function to look up an object in an array:

Javascript Generate Dynamic Key Values In An Object Examples

DISCLAIMER: As basically everyone said in the comments, yes, you could use the filter function instead of find, it's equally simple, but the point of this article here is to get to know about the find function. If you don't know about the filter function and want to have a look on it instead, I also wrote this small article talking about it. =)

It makes thing easier than ever, HOWEVER, you should already know that ES6 is not supported by some older browsers, so you should pay a little attention to this. One of the things that you can do to avoid browser-compatibility problems when using ES6-only functions is to use an external javascript library such as Underscore.js, because such libraries implement ES6 functions in a way that allow us to use these functions without worrying about older browsers support (and as a plus, those kind of libraries may even have some additional cool functions too). Hitman absolution cd key generator steam.

Using Underscore.js

If we opt to use Underscore.js, our code to find the object with the id equal to 3 changes to this:

Using jQuery

Maybe you already use jQuery in your project, and if that is the case, you can use jQuery’s grep function to achieve our desired result, it goes like this:

Using AngularJS $filter

If you’re already working with AngularJS, you can also use the $filter module to filter the array. Using AngularJS’s $filter, it looks like this:

Here’s a demo on jsFiddle for you to play around with.

Key Values In Life

I hope you enjoyed this short post, keep up the good (and clean) code!

Javascript Dynamic String

;)

Coments are closed
Scroll to top