megacolorboy

Back

Published on August 13th, 2021

JavaScript

How to fetch a JSON object from an array of JSON objects by property value?

Say, for example, you have an array of JSON objects that contains the following data:Hmm, that's pretty basic but how will get the information of "Bob Smith" using his email? Well, that's where the method comes into the picture.Try reading the Mozilla Documentation on and implement the following:This works fine on all browsers except Internet Explorer (I mean, it sucks anyways!), which is okay!Hope you found this useful!

1 minute read

Say, for example, you have an array of JSON objects that contains the following data:


var arrOfObjects = [
    {
        name: "John Doe",
        age: 20,
        email: "john.doe@email.com"
    },
    {
        name: "Bob Smith",
        age: 56,
        email: "bob.smith@email.com"
    },
];

Hmm, that's pretty basic but how will get the information of "Bob Smith" using his email? Well, that's where the Array.find() method comes into the picture.

Try reading the Mozilla Documentation on Array.prototype.find() and implement the following:


var key = "email";
var valueToFind = "bob.smith@email.com";
var result = arrOfObjects.find(obj => {
    return obj[key] == value;
});

//This should give you the record of Bob Smith.
console.log(result) 

This works fine on all browsers except Internet Explorer (I mean, it sucks anyways!), which is okay!

Hope you found this useful!