对于此对象数组:
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"]
}
];
然后有一个for
循环,该循环遍历对象数组以及用于针对对象进行查找的参数。我知道我们正在迭代,并且该函数将在returns
之后停止运行。我添加了console.log(i);
来查看迭代器变量的值,以查看循环发生了多少次。
function lookUpProfile(name, prop) {
for (var i = 0; i < contacts.length; i++) {
console.log(i);
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
} else {
return "No such property";
}
}
} //end of loop
return "No such contact";
}
console.log(lookUpProfile("Kristian", "lastName")); // Vos
因此,我运行代码并查看控制台输出并获得期望的结果,我们进行4次迭代,然后return
当前对象的属性。
为什么当我重构函数时代码不起作用,所以我没有逻辑嵌套的if
,而是使用了逻辑AND运算符&&
,它将两个测试条件合并为一个if
语句-像这样:
function lookUpProfile(name, prop) {
for (var i = 0; i < contacts.length; i++) {
console.log(i);
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
} else {
return "No such property";
}
} //end of loop
return "No such contact";
}
console.log(lookUpProfile("Kristian", "lastName")); // Expected output: Vos
现在,当我运行代码时,似乎没有迭代发生,因为console.log(i);
似乎一次undefined
输出了一次,而不是多次输出,所以我们似乎没有在循环,我也不明白为什么。其次,即使满足我们的测试条件,我们也将返回“无此类属性”。
如果这个简单的重构为什么破坏了功能,我需要了解什么。我知道一旦函数返回某些结果,函数执行将结束并且迭代将停止,我认为此问题与循环外和控制流中断有关,但是我不明白为什么。请有人可以清楚地说明为什么在我的特定情况下会发生这种情况。
答案 0 :(得分:1)
如果您嵌套了if条件
if (contacts[i].firstName === name)
这将检查名称,如果名称不匹配则不执行任何操作
但是在第二个例子中,如果名称不匹配,则在else语句后返回else语句
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
} else { // problem is here
return "No such property";
}
只需从循环中删除else条件
var contacts = [{"firstName": "Akira","lastName": "Laine","number": "0543236543","likes": ["Pizza", "Coding", "Brownie Points"]},{"firstName": "Harry","lastName": "Potter","number": "0994372684","likes": ["Hogwarts", "Magic", "Hagrid"]},{"firstName": "Sherlock","lastName": "Holmes","number": "0487345643","likes": ["Intriguing Cases", "Violin"]},{"firstName": "Kristian","lastName": "Vos","number": "unknown","likes": ["JavaScript", "Gaming", "Foxes"]}];
function lookUpProfile(name, prop) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
}
} //end of loop
return "No such contact";
}
console.log(lookUpProfile("Kristian", "lastName"));
答案 1 :(得分:0)
因为在您之前的代码中,仅当"No such property"
为contacts[i].hasOwnProperty(prop)
时才返回false
。现在,如果contacts[i].firstName === name
为假,也将返回它。您希望将这两个if
块分开。
此外,您的console.log(i)
打印undefined
的原因是,它位于for
变量声明之前的i
循环之上。将其向下移动1行。
答案 2 :(得分:0)
return
将停止循环。
在您的情况下,由于名称或属性不匹配,循环在第一次迭代后停止
var contacts = [{"firstName":"Akira","lastName":"Laine","number":"0543236543","likes":["Pizza","Coding","Brownie Points"]},{"firstName":"Harry","lastName":"Potter","number":"0994372684","likes":["Hogwarts","Magic","Hagrid"]},{"firstName":"Sherlock","lastName":"Holmes","number":"0487345643","likes":["Intriguing Cases","Violin"]},{"firstName":"Kristian","lastName":"Vos","number":"unknown","likes":["JavaScript","Gaming","Foxes"]}];
function lookUpProfile(name, prop) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
}
} //end of loop
return "No such contact";
}
console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Kristian1", "lastName"));