2

我正在研究 codeAcademy 的 javascript 部分,我非常坚持其中一项练习。这是我的代码:

var friends = {
    bill:{
        firstName:"Bill",
        lastName: "Gates",
        number:"(206)555-5555",
        address:['One Microsoft Way', 'Redmond','wa','12345']
    }
};
var friends = {
    steve:{
        firstName:"Steve",
        lastName: "Jobs",
        number:"(555)555-5555",
        address:['One Apple Way', 'San Diego','ca','12345']
    }
};

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

var search = function(name)
{
    for (var contact in friends)
    {
        if (contact == name)
        {
            for (var contact_info in friends.name)
            {
            console.log(contact_info);
            }
        }
    }
}

我对 for/in 循环很困惑。我一直在做这个练习一段时间,想知道是否有人可以帮助我理解。主要是搜索功能。它假设能够查看给定名称是否在对象中并打印关联的联系信息。我完全迷路了。我什至尝试重新启动整个部分,但仍然卡住了。

4

3 回答 3

1

在 Javascript 中,for-in 循环将遍历对象的每个属性。

你的朋友对象首先被创建,它只有一个属性,它也是一个对象,描述比尔盖茨。然后它被另一个对象覆盖,该对象也只有一个属性,但现在描述的是上帝史蒂夫乔布斯。

最后,在搜索功能中,您将浏览朋友的每个属性并将它们与字符串进行比较。在for-in循环内部,name是一个变量,其中包含循环当前迭代中使用的属性的名称。因此,如果您使用变量的名称(即:),您将得到匹配steve。如果您希望匹配存储在对象中的名称,则必须使用未在函数name声明中调用的参数search,并进行如下检查:

if (contact.firstName == _name || contact.lastName == _name ||
    (contact.firstName + " " + contact.lastName) == _name)

另请注意,在您friends使用 Bill 创建变量后,您将使用 Steve 重新创建它。因此,您最终在该变量中只有一个“联系人”。您可以像这样更改对象声明:

var friends = {}; // This creates an empty object

friends.bill = {
    firstName: "Bill",
    lastName: "Gates" // And so on...
};

friends.steve = {
    firstName: "Steve" // You get the drill
};

然后你的朋友反对现在有两个硅谷的海盗。

于 2013-08-15T14:09:36.257 回答
0

首先你有错误的朋友变量定义,你用史蒂夫覆盖比尔。

应该和这个类似

var friends = {
    bill:{
        firstName:"Bill",
        lastName: "Gates",
        number:"(206)555-5555",
        address:['One Microsoft Way', 'Redmond','wa','12345']
    },
    steve:{
        firstName:"Steve",
        lastName: "Jobs",
        number:"(555)555-5555",
        address:['One Apple Way', 'San Diego','ca','12345']
    }
};

现在回答你的问题。

function search(name)
{
    // Length of friends  object received using Object.keys
    for (var p = 0; p < Object.keys(friends).length ;p++)
    {
        if(friends[name] != undefined){
            // Add to console and stop function execution if match found.
            console.log(friends[name].firstName); 
            return;
        }
    }
}

一些解释。如果 "friends[name]" 未定义,则表示您没有具有指定名称的对象。如果它返回某个对象,您只需获取“firstName”的值或对象中的任何其他属性。

于 2013-08-15T14:22:39.543 回答
0

A for in loop in Javascript will loop through each property of an object. Unfortunately, your object will only have 1 property steve because you overwrite the entire object with your second var friends = ... statement. When looping through an object, your name variable will be the string index of your friend in the friends object. Once you find a match for the name, you can use the contact information by using friends[name]. If you are looking for a friend with the same first name as your search, you may want to look at the specific first name

Just as a side note, because Javascript is a loosely typed language, there is no need to loop through the entire friends object to see if you have a friend with that name. Here is a code sample:

var friends = {
    bill: {
        firstName: 'Bill', 
        lastName: 'Gates'
    }, 
    steve: {
        firstName: 'Steve', 
        lastName: 'Jobs'
    }, 
    steve2: {
        firstName: 'Steve', 
        lastName: 'Doe'
    }
}, 
search1 = function(name) {
    if(friends[name] !== undefined) { //Is there a friend with this index
        //Yay, we have a friend with this index!
    }
    else {
        //Sorry, we don't have a friend with this index
    }
}, 
search2 = function(name) {
    name = name.toLowerCase(); //Case insensitive
    for(var friend in friends) {
        if(friends[friend].firstName.toLowerCase() == name) { //Does this person have the same first name
            //This friend has the same first name
        }
    }
};

search1('steve'); //Will only give us Steve Jobs
search2('steve'); //Will give us Steve Jobs and Steve Doe
于 2013-08-15T14:28:09.457 回答