我對javascript很陌生,我一直在努力應對這種挑戰。我了解問題并實施了解決問題的邏輯,但我無法正確顯示這些值。
有人可以看看我的代碼并指出我正確的方向嗎?
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"
uj5u.com熱心網友回復:
您應該更改results = elm[field];為if (elm[field]) { results[field] = elm[field]; results.name = elm.name; },這是因為您需要驗證該屬性是否存在。如果 results 為空意味著在聯系人串列中未找到該朋友或未在該人中設定 searched 屬性,您還應該更改return results; 。return Object.keys(results).length ? results : "Not found";
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"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/473970.html
標籤:javascript 目的 调试
