gpt4 book ai didi

javascript - 为什么下面的javascript代码会打印3次结果?

转载 作者:行者123 更新时间:2023-11-28 08:13:47 26 4
gpt4 key购买 nike

“steve”联系人在控制台中打印了 3 次。

var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "555555555",
address: ["One Infinite Loop", "Cupertino", "CA", "98052"]
},
bill: {
firstName: "Bill",
lastName: "Gates",
number: "555555555",
address: ["One Microsoft way", "Redmond", "WA", "98052"]
}
};

var list = function (friends) {
for (var firstName in friends) {
console.log(firstName);
}
};

var search = function (name) {
for (var steve in friends) {
if (friends[steve].firstName === name) {
console.log (friends[steve]);
return friends [steve];
}
}
};

说明:定义一个采用单个参数名称的函数搜索。如果传递给函数的参数与 friend 中的任何名字匹配,它应该将该 friend 的联系信息记录到控制台并返回。

最佳答案

var search = function(name) {
for(var name in friends) {
if(friends[name].firstName) {
console.log(friends[name]);
return friends[name];
}
}
};

这是通往胜利的桥梁...尝试一下!

关于javascript - 为什么下面的javascript代码会打印3次结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23845007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com