Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Unable to retrieve the correct value from a list of object within object

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: "laurel@comics.com",
    friends: ["Hardy", "Abbott", "Costello"],
  },
  {
    name: "Hardy",
    phone: "321 654 0987",
    email: "hardy@hardyharhar.com",
    friends: ["Laurel", "Buster"],
  },
  {
    name: "Buster",
    phone: "987 654 3210",
    email: "buster@keaton.ca",
    friends: ["Hardy"],
  },
  {
    name: "Abbott",
    phone: "888 123 4567",
    email: "abbott@whosonfirst.co",
    friends: ["Costello", "Laurel"],
  },
  {
    name: "Costello",
    phone: "767 676 7676",
    email: "costello@imonfirst.co",
    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: "hardy@hardyharhar.com"}
console.log(findFriend(contacts, "Bob", "phone")); // returns "Not found"
console.log(findFriend(contacts, "Costello", "birthday")); // returns "Not found"
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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: "laurel@comics.com",
    friends: ["Hardy", "Abbott", "Costello"],
  },
  {
    name: "Hardy",
    phone: "321 654 0987",
    email: "hardy@hardyharhar.com",
    friends: ["Laurel", "Buster"],
  },
  {
    name: "Buster",
    phone: "987 654 3210",
    email: "buster@keaton.ca",
    friends: ["Hardy"],
  },
  {
    name: "Abbott",
    phone: "888 123 4567",
    email: "abbott@whosonfirst.co",
    friends: ["Costello", "Laurel"],
  },
  {
    name: "Costello",
    phone: "767 676 7676",
    email: "costello@imonfirst.co",
    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: "hardy@hardyharhar.com"}
console.log(findFriend(contacts, "Bob", "phone")); // returns "Not found"
console.log(findFriend(contacts, "Costello", "birthday")); // returns "Not found"

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!