RogerFedFan
RogerFedFan

Reputation: 566

Check if value doesn't exist in array of objects

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");

Upvotes: 1

Views: 11910

Answers (6)

oj pr
oj pr

Reputation: 1

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");

Upvotes: 0

Firsty Ukhti Molyndi
Firsty Ukhti Molyndi

Reputation: 53

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");

Upvotes: 0

le_m
le_m

Reputation: 20248

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];
}

Upvotes: 0

ESCM
ESCM

Reputation: 262

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));

Upvotes: 0

Daniel Apt
Daniel Apt

Reputation: 2658

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];
}

Upvotes: 5

orabis
orabis

Reputation: 2809

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");

Upvotes: 3

Related Questions