Codecademy上有一节课,指导我创建一个联系人列表(在对象中包含对象)。最后,我要创建一个可以输入并与联系人列表上的任何名称匹配的函数。如果匹配,则会将与该名称关联的所有联系信息记录到控制台。
到目前为止,我已经成功完成了上述所有工作,并希望更进一步。我希望该函数能够记录每个值之间间隔的人的地址(例如:“One Microsoft Way,Redmond,WA,98052”而不是“One Microsoft Way,Redmond,WA,98052”)。
我还希望该功能能够记录“不匹配”。进入控制台(只需一次)如果它搜索整个联系人列表并找不到匹配的名称。使用我当前的代码,它会记录“不匹配”。对于每个不匹配的名称,我不希望这样。
简而言之:如何在每个值之间以间距返回和分隔值数组?如何让函数返回“不匹配”。没有找到任何匹配后的一次?
这是我的代码:
// Main object
var friends = {
// Nested objects
bill: {
firstName: "Bill",
lastName: "Pifheba",
number: "1234567890",
address: ["One Microsoft Way", "Redmond", "WA", "98052"]
},
steve: {
firstName: "Steve",
lastName: "Ajfiae",
number: "1234512345",
address: ["2 Stack Way", "Lazss", "WA", "12345"]
},
bob: {
firstName: "Bob",
lastName: "Dcfiwae",
number: "8291047261",
address: ["28 Stack Way", "What", "WA", "54321"]
}
};
// Searches for matching name
var search = function(name) {
// Iterates through nested objects
for (var x in friends) {
// If match is found
if (friends[x].firstName === name) {
// Print contact information
console.log("First Name: " + friends[x].firstName + "\r\nLast Name: " + friends[x].lastName + "\r\nNumber: " + friends[x].number + "\r\nAddress: " + friends[x].address);
} else {
console.log("No match.");
}
}
};
// Asks for who to search for
search(prompt("Name?"));
答案 0 :(得分:1)
您可以.join
数组将其转换为以空格分隔的字符串。
["One Microsoft Way", "Redmond", "WA", "98052"].join(", ");
// "One Microsoft Way, Redmond, WA, 98052"
不是在函数中记录值,而是可以返回它,而是记录调用它的结果。
if (friends[x].firstName === name) {
return "First Name: " + friends[x].firstName + "\r\nLast Name: " + friends[x].lastName + "\r\nNumber: " + friends[x].number + "\r\nAddress: " + friends[x].address);
}
一旦找到匹配,这将使循环短路。要提供回退返回值,只需在函数末尾添加另一个return语句。
var search = function(name) {
// ...
return "No match.";
}
只有在循环完成且未找到匹配项时,才会运行此语句。
最后,您可以直接记录通话结果。
console.log(search(prompt("Name?")));
答案 1 :(得分:0)
var search = function(name) {
// Iterates through nested objects
var found = false;
for (var x in friends) {
// If match is found
if (friends[x].firstName === name) {
// Print contact information
found = true;
console.log("First Name: " + friends[x].firstName + "\r\nLast Name: " + friends[x].lastName + "\r\nNumber: " + friends[x].number + "\r\nAddress: " + friends[x].address.join(' '));
break; // stop iteration
}
}
if (!found) {
console.log("No match.");
}
};
答案 2 :(得分:0)
您可以使用Array.prototype.join()
连接数组元素和提供给方法的字符串(此处为', '
),以形成字符串:
friends[x].address.join(', ')
参考文献:
答案 3 :(得分:0)
您的代码存在一些问题。
friends
,因为每个密钥都是唯一的请参阅以下代码中的评论
// Main object
var friends = {
// Nested objects
bill: {
firstName: "Bill",
lastName: "Pifheba",
number: "1234567890",
address: ["One Microsoft Way", "Redmond", "WA", "98052"]
},
steve: {
firstName: "Steve",
lastName: "Ajfiae",
number: "1234512345",
address: ["2 Crap Way", "Lazss", "WA", "12345"]
},
bob: {
firstName: "Bob",
lastName: "Dcfiwae",
number: "8291047261",
address: ["28 Crap Way", "What", "WA", "54321"]
}
};
// Searches for matching name
var search = function(name) {
// check to see if the friends name is a property of your object
// if not return early
if ( name in friends == false ) {
console.log('not found');
return false;
}
// since you have already checked that the name exists you
// can just use the name property instead of looping through
// as all keys must be unique when using an object
console.log( friends[name] );
console.log("First Name: " + friends[name].firstName + "\r\nLast Name: " + friends[name].lastName + "\r\nNumber: " + friends[name].number + "\r\nAddress: " + friends[name].address.join(' '));
// you can join an array with `.join`
console.log(friends[name].address.join(' '));
};
// Asks for who to search for
search(prompt("Name?"));
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>