简体   繁体   English

为什么我只能在javascript中打印一个对象?

[英]Why am I only able to print one object in javascript?

Doing some practice runs on codecademy, and came across the following problem: 做一些练习在codecademy上运行,并遇到以下问题:

I am able to only console.log "Steve Jobs" and all his info, but I want to also 我仅能console.log“史蒂夫·乔布斯”及其所有信息,但我也想

include "Bill Gates." 包括“比尔·盖茨”。 If anyone knows how to do this that would be great, or 如果有人知道该怎么做,那就太好了,或者

any alternatives to the following code: 以下代码的任何替代方法:

var friends = {};

friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "(206)555-5555",
    address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
};


friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(206)777-7777",
    address: ['Apple Rd.', 'Cupertino', 'CA', '90210']
};

var list = function(list) {

    for (var dale in friends) {
        console.log(dale);
    }

}

var search = function(name) {
    for (var key in friends) {

        if (name === friends[key].firstName) {
            console.log(friends[key]);
            return friends[key];
        }
    }
};

OK, so when I run this code, only Steve gets printed. 好的,所以当我运行这段代码时,只会打印出史蒂夫。 It should also print Bill. 它还应打印条例草案。

Copy/pasted the code in the console and calling: 在控制台中复制/粘贴代码并调用:

search('Bill');

Works just fine for me, how are you using this code? 对我来说效果很好,您如何使用此代码?

Created a JSBin from your code with the addition that I called list(); 根据您的代码创建了一个JSBin ,另外我还调用了list(); right at the end. 就在最后。 If you run the code using CMD+Enter you will see that the output is as expected, printing out both bill and steve 如果使用CMD+Enter运行代码,您将看到输出符合预期,同时打印出BillSteve

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM