1

I have to write a function that checks to see whether argument 1 (firstName) is a value in the array oj objects stored in variable (contacts) and if it is, return the current objects property value (prop). If firstName is not present, I have to return 'No contact found'. If a prop is not found, I have to return 'No such property'.

I'm having a hard time figuring out how to check whether the argument (firstName) is not found in the object of arrays. How does one write an if statement to check for that?

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",
 'youtube': 'nah',
 "number": "0487345643",
 "likes": ["Intriguing Cases", "Violin"]
 },
 {
 "firstName": "Kristian",
 "lastName": "Vos",
 "number": "unknown",
 "likes": ["Javascript", "Gaming", "Foxes"]
 }
];
function lookUpProfile(firstName, prop){
 for (i=0;i<contacts.length;i++) {
 if (Object.values(contacts[i]).indexOf(firstName) > -1 && contacts[i].hasOwnProperty(prop)) {
 return contacts[i][prop];
 }
 if (!contacts[i].hasOwnProperty(prop)) {
 return 'No such property';
 }
 } 
}
lookUpProfile("Sherlock", "likes");

asked Jan 7, 2018 at 19:50
5
  • contacts.find(obj => obj.firstName === firstName)[prop]. Add intermediate truthy checking for No contacts found and No property found Commented Jan 7, 2018 at 19:52
  • I believe its convention to surround property keys in "quotes" in JSON, but I think its standard practice to leave your keys without them. i.e. instead of "firstName": "Kristian", have firstName: "Kristian". Commented Jan 7, 2018 at 19:57
  • @Li357 find has kinda anemic support, (contacts.filter(obj => obj.firstName === firstName)[0] || {})[prop] || 'No such property'; works in all browsers including IE 9. Although admittedly less readable. Commented Jan 7, 2018 at 19:59
  • @JoelBiffin doesn't matter, question of taste Commented Jan 7, 2018 at 20:00
  • What shall the function return when there are two contacts with the wanted firstName, but only the second one has the wanted property? Commented Jan 7, 2018 at 20:02

6 Answers 6

5

You use Array.find to find the object with the correct firstName. You would use it as follows:

function lookUpProfile(firstName, prop) {
 var profile = contacts.find(function(contact) {
 return contact.firstName === firstName;
 });
 if (!profile) {
 console.log("no profile found for " + firstName);
 return;
 }
 if (!profile[prop]) {
 console.log("no " + prop + " found on " + firstName + " profile");
 return;
 }
 return profile[prop];
}
answered Jan 7, 2018 at 20:03

Comments

3

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",
 'youtube': 'nah',
 "number": "0487345643",
 "likes": ["Intriguing Cases", "Violin"]
 },
 {
 "firstName": "Kristian",
 "lastName": "Vos",
 "number": "unknown",
 "likes": ["Javascript", "Gaming", "Foxes"]
 }
];
function lookUpProfile(firstName, prop) {
 var filter = contacts.filter(a => a.firstName === firstName);
 if (!filter.length) {
 return console.log('No contact found');
 }
 
 filter = filter.filter(a=> prop in a);
 
 if (!filter.length) {
 return console.log('No such property');
 }
 
 return filter;
}
lookUpProfile("Sherlock", "likes");
lookUpProfile("Sherlock", "hates");
lookUpProfile("Mycroft", "likes");

answered Jan 7, 2018 at 20:02

Comments

0

var check = (first, prop) => (obj.map(cur => cur.firstName === first ? cur.firstName : "Not found")[0]);
var obj = [{
 firstName: "Eduardo",
 age: 17
}];
console.log(check(obj[0].firstName));

answered Jan 7, 2018 at 20:04

Comments

0

Your current approach doesn't allow to distinguish between the two error cases as your try to combine both conditions - name and property - in a single if condition within the loop body.

Also, your approach doesn't allow the user of the lookUpProfile function to distinguish between a real error and the special case when the prop exists and has the value 'No contact found' or 'No such property'. You might be better off throwing an exception instead of returning an error string.

A working approach is to first filter the candidates with matching name and then find the first candidate having the required property:

function lookUpProfile(firstName, prop) {
 const candidates = contacts.filter(contact => contact.firstName === firstName);
 if (candidates.length === 0) throw new Error('No contact found');
 const match = candidates.find(candidate => candidate.hasOwnProperty(prop));
 if (!match) throw new Error('No such property');
 return match[prop];
}
answered Jan 7, 2018 at 20:10

Comments

0

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",
 'youtube': 'nah',
 "number": "0487345643",
 "likes": ["Intriguing Cases", "Violin"]
 },
 {
 "firstName": "Kristian",
 "lastName": "Vos",
 "number": "unknown",
 "likes": ["Javascript", "Gaming", "Foxes"]
 }
];
function lookUpProfile(firstName, prop){
 for (i=0;i<contacts.length;i++) {
 if (Object.values(contacts[i]).indexOf(firstName) > -1 && contacts[i].hasOwnProperty(prop)) {
 return contacts[i][prop];
 }
 if (!contacts[i].hasOwnProperty(prop)) {
 return 'No such property';
 }
 } 
}
lookUpProfile("Sherlock", "likes");

answered Aug 6, 2021 at 7:17

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
Hi and welcome to stackoverflow, here are some guidelines : it is not recommended to post code-only answers, answers should provide more explanation about the code to make it the answer more useful and are more likely to attract upvotes
0

You can try this:

The first If determines:

1.If name is in contacts AND if (prop) is a property.
2.If both conditions are true.
3.Then return that VALUE!.

The else if determines:

1.If name is in contacts AND (prop) is not a property.
2.If both conditions are true.
3.Then returns "No such property".

The last return determines: That for everything else, including a name not in the contacts, return "No such contact".

// Setup
const 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(name, prop) {
 // Only change code below this line 
 for (let i = 0; i < contacts.length; i++) {
 
 if ((name === contacts[i].firstName) && (contacts[i].hasOwnProperty(prop) === true)){
 console.log(contacts[i][prop]);
 return contacts[i][prop];
 }
 else if((name === contacts[i].firstName) &&(contacts[i].hasOwnProperty(prop) === false ) ){
 return "No such property";
 }
 }
 return "No such contact";
 
 
 }
 
 // Only change code above this line
lookUpProfile("Sherlock", "likes");

answered Jun 15, 2022 at 6:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.