无法从对象中的对象列表中检索正确的值

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

我是 javascript 的新手,我一直在努力应对这类挑战。我了解问题并实施逻辑来解决问题,但我无法正确显示值。

有人可以看一下我的代码,并为我指出正确的方向吗??

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"

您应该将 results = elm[field]; 更改为 if (elm[field]) { results[field] = elm[field]; results.name = elm.name; }, 这是因为您需要验证 属性 是否存在。你也应该把return results; 改成return Object.keys(results).length ? results : "Not found";如果结果为空意味着在联系人列表中没有找到朋友或者搜索到的属性不是那个人。

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"