Home > Back-end >  Unable to retrieve the correct value from a list of object within object
Unable to retrieve the correct value from a list of object within object

Time:05-13

I am quite new to javascript and I've been struggling with this type of challenge. I understand the problem and implement a logic to solve the issue but I am unable to display the values correctly.

Can someone take a look at my code and maybe point me to the correct direction??

const contacts = [
  {
    name: "Laurel",
    phone: "123 456 7890",
    email: "[email protected]",
    friends: ["Hardy", "Abbott", "Costello"],
  },
  {
    name: "Hardy",
    phone: "321 654 0987",
    email: "[email protected]",
    friends: ["Laurel", "Buster"],
  },
  {
    name: "Buster",
    phone: "987 654 3210",
    email: "[email protected]",
    friends: ["Hardy"],
  },
  {
    name: "Abbott",
    phone: "888 123 4567",
    email: "[email protected]",
    friends: ["Costello", "Laurel"],
  },
  {
    name: "Costello",
    phone: "767 676 7676",
    email: "[email protected]",
    friends: ["Abbott", "Laurel"],
  },
];

function findFriend(contacts, name, field) {
  let results = {};

  contacts.forEach(function (elm) {
// loop through all contacts and look for name
    if (elm.name === name) {
// select the first friend
      let friend = elm.friends[0];

      contacts.forEach((elm) => {
// looking for the friend in the contacts object
        if (elm.name === friend) {
// when found - this will get the required field and write it to the result
          results = elm[field]; 
        }
      });
    }
  });
  return results; // return the results
}

/

/ Test cases

console.log(findFriend(contacts, "Abbott", "phone")); // returns {name: "Costello", phone: "767 676 7676"}
console.log(findFriend(contacts, "Buster", "email")); // returns {name: "Hardy", email: "[email protected]"}
console.log(findFriend(contacts, "Bob", "phone")); // returns "Not found"
console.log(findFriend(contacts, "Costello", "birthday")); // returns "Not found"

CodePudding user response:

You should change results = elm[field]; to if (elm[field]) { results[field] = elm[field]; results.name = elm.name; }, this because you need to verify that property exist. You should also change return results; by return Object.keys(results).length ? results : "Not found"; if results is empty means that the friend was not found in the contacts list or that the searched property is not set in that person.

const contacts = [
  {
    name: "Laurel",
    phone: "123 456 7890",
    email: "[email protected]",
    friends: ["Hardy", "Abbott", "Costello"],
  },
  {
    name: "Hardy",
    phone: "321 654 0987",
    email: "[email protected]",
    friends: ["Laurel", "Buster"],
  },
  {
    name: "Buster",
    phone: "987 654 3210",
    email: "[email protected]",
    friends: ["Hardy"],
  },
  {
    name: "Abbott",
    phone: "888 123 4567",
    email: "[email protected]",
    friends: ["Costello", "Laurel"],
  },
  {
    name: "Costello",
    phone: "767 676 7676",
    email: "[email protected]",
    friends: ["Abbott", "Laurel"],
  },
];

function findFriend(contacts, name, field) {
  let results = {};

  contacts.forEach(function (elm) {
    // loop through all contacts and look for name
    if (elm.name === name) {
      // select the first friend
      let friend = elm.friends[0];

      contacts.forEach((elm) => {
        // looking for the friend in the contacts object
        if (elm.name === friend) {
          // when found - this will get the required field and write it to the result
          if (elm[field]) {
            results[field] = elm[field];
            results.name = elm.name;
          }
        }
      });
    }
  });
  return Object.keys(results).length ? results : "Not found"; // return the results
}

console.log(findFriend(contacts, "Abbott", "phone")); // returns {name: "Costello", phone: "767 676 7676"}
console.log(findFriend(contacts, "Buster", "email")); // returns {name: "Hardy", email: "[email protected]"}
console.log(findFriend(contacts, "Bob", "phone")); // returns "Not found"
console.log(findFriend(contacts, "Costello", "birthday")); // returns "Not found"

  • Related