Javascript Object Array find by object attribute
//Setup/* w w w .j a v a 2s.com*/ var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number": "0543236543", "likes": ["Pizza", "Coding", "Brownie Points"] }, { "firstName": "Harry", "lastName": "Potter", "number": "0994372684", "likes": ["Hogwarts", "Magic", "Hagrid"] }, { "firstName": "Sherlock", "lastName": "Holmes", "number": "0487345643", "likes": ["Intriguing Cases", "Violin"] }, { "firstName": "Kristian", "lastName": "Vos", "number": "unknown", "likes": ["Javascript", "Gaming", "Foxes"] } ]; function lookUpProfile(firstName, prop){ var i = 0; console.log(prop); while (i < contacts.length) { if ((contacts[i].firstName == firstName) && (contacts[i].hasOwnProperty(prop))) { console.log("found"); return contacts[i][prop]; } if (!contacts[i].hasOwnProperty(prop)) { return ("No such property"); } i += 1; } return ("No such contact"); } // Change these values to test your function console.log(lookUpProfile("Akira", "likes")); console.log(lookUpProfile("Kristian", "lastName")); console.log(lookUpProfile("Sherlock", "likes")); console.log(lookUpProfile("Harry", "likes")); console.log(lookUpProfile("Bob", "number")); console.log(lookUpProfile("Akira", "address"));