I have the following array of objects:
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"]
}
];
I want to define a function, lookUpProfile(firstName, prop)
, which for the input data:
lookUpProfile("Akira", "likes");
will return:
["Pizza", "Coding", "Brownie Points"]
This is my solution so far which doesn't return something till now:
function lookUpProfile(firstName, prop){
for(var i = 0; i < contacts.length; i++){
if(contacts[i].firstName == firstName){
for(var j = 0; j < contacts[i].length; j++){
if(contacts[i].hasOwnProperty(prop))
return prop;
}
}
}
}
Has anyone any idea to solve this? Thanks
You can try to do like this. Find the item with .find()
function (ES6) and return the appropriate property using bracket []
syntax.
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 lookUpProfileES6(name, prop){
var found = contacts.find(item => item.firstName === name);
if(found){
return found[prop];
}
}
function lookUpProfileES5(name, prop){
var found = null;
for(var i = 0; i < contacts.length; i++){
if(contacts[i].firstName === name){
found = contacts[i];
break;
}
}
if(found){
return found[prop];
}
}
var resultES6 = lookUpProfileES6("Akira", "likes");
var resultES5 = lookUpProfileES5("Akira", "likes");
console.log(resultES6);
console.log(resultES5);
You problem is that you are trying to loop over object with for loop with numeric indeces. You don't need second loop at all. It could be as simple as
function lookUpProfile(firstName, prop) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
}
}
}
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) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop))
return contacts[i][prop];
}
}
}
console.log(lookUpProfile("Akira", "likes"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With