Read value from a JSON Array by referring to another Key’s value JavaScript

Suppose we have an JSON array as below.

var details = [

{“name”:x,”email”:”test@gmail.com},

{“name”:y,”email”:”test1@gmail.com},

]

To get Y’s email id use the below function

function findElement(arrayName, nameValue) {
    for (var i = 0; i < arr.length; i++)

        if (arr[i]["name"] == propValue)
            return arr[i]["email"];

    // will return undefined if not found; you could return a default instead
}

Leave a comment

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