0 points

Search through an array of objects


dashboard

Thu Aug 06 2020 15:57:12 GMT+0000 (UTC)

Posted by @MarkJason #javascript

function search(nameKey, myArray){
    for (var i=0; i < myArray.length; i++) {
        if (myArray[i].name === nameKey) {
            return myArray[i];
        }
    }
}

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

var resultObject = search("string 1", array);
content_copy Copy

https://stackoverflow.com/questions/12462318/find-a-value-in-an-array-of-objects-in-javascript