JavaScript - 在数组中存储多个对象并通过访问它们的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37077617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:52:12  来源:igfitidea点击:

JavaScript - Store multiple objects in array and access their properties via

javascriptarraysobjectpush

提问by mellows

So here I have a database of user objects and I would like to push each one into the 'allUsers' array. I then want to be able to loop through this array and access a property e.g. allUsers[i].genrePref. How do I push all of these variables into the array to store them like [user, jon, lucy, mike, luke...

所以这里我有一个用户对象数据库,我想将每个对象都推送到“allUsers”数组中。然后我希望能够遍历这个数组并访问一个属性,例如allUsers[i].genrePref. 我如何将所有这些变量推入数组以存储它们[user, jon, lucy, mike, luke...

    // Users
    var user = {username: user9110252username, genrePref: user9110252genre};
    var jon = {username: 'Jon', genrePref: 'rock'};
    var lucy = {username: 'Lucy', genrePref: 'pop'};
    var mike = {username: 'Mike', genrePref: 'rock'};
    var luke = {username: 'Luke', genrePref: 'house'};
    var james = {username: 'James', genrePref: 'house'};
    var dave = {username: 'Dave', genrePref: 'bass'};
    var sarah = {username: 'Sarah', genrePref: 'country'};
    var natalie = {username: 'Natalie', genrePref: 'bass'};


    // Users array
    var allUsers = [];

回答by Danny Bullis

To just add the variablesto the array, there's really no other way except to do exactly what you did in your example, which is to manually add each variable to your array.

要将变量添加到数组中,除了完全按照您在示例中所做的操作(即手动将每个变量添加到数组中)之外,确实没有其他方法。

// Users array
var allUsers = [
    user,
    jon,
    lucy,
    mike,
    luke,
    james,
    dave,
    sarah,
    natalie
];

BUT, you should probably do something a little more robust, which in its simplest form is to define an object literal called usersthat contains each user object. Then you not only have the added benefit of a pseudo-namespacing around your users, you can easily iterate over the object literal and put each user into an array, as such:

但是,您可能应该做一些更健壮的事情,最简单的形式是定义一个称为users包含每个用户对象的对象字面量。然后,您不仅可以在用户周围获得伪命名空间的额外好处,还可以轻松地遍历对象文字并将每个用户放入一个数组中,如下所示:

// Users
var users = {
    user : {username: user9110252username, genrePref: user9110252genre},
    jon : {username: 'Jon', genrePref: 'rock'},
    lucy : {username: 'Lucy', genrePref: 'pop'},
    mike : {username: 'Mike', genrePref: 'rock'},
    luke : {username: 'Luke', genrePref: 'house'},
    james : {username: 'James', genrePref: 'house'},
    dave : {username: 'Dave', genrePref: 'bass'},
    sarah : {username: 'Sarah', genrePref: 'country'},
    natalie : {username: 'Natalie', genrePref: 'bass'}
}

// Users array
var allUsers = [];

// Populate users array
for(var key in users) {
    allUsers.push(users[key]);
}

One last note on style. You should probably avoid explicitly setting the 'key' of each user object in your usersobject literal to be the nameof the user. If you have any users with the same name, this model will break. Instead, consider generating some kind of unique key, like a guid, or some other unique value for each user. That way you'll never accidentally overwrite an entry in your usersobject if, for example, you are populating this usersobject from some database and two or more users happen to have the same name.

关于风格的最后一点。您可能应该避免将users对象文字中每个用户对象的“键”显式设置为用户。如果您有任何同名的用户,则此模型将中断。相反,请考虑为每个用户生成某种唯一键,例如 guid 或其他一些唯一值。这样您就永远不会意外覆盖users对象中的条目,例如,如果您users从某个数据库填充此对象并且两个或多个用户碰巧具有相同的名称。

Good luck.

祝你好运。

回答by omarjmh

Working Example

工作示例

Just use .pushto push all of them in like so:

只需.push像这样将它们全部推入即可:

var allUsers = [];

allUsers.push(allUsers, user, jon, lucy, mike, luke, james, dave, sarah, natalie);

回答by hmharsh3

  var arr = [{username: 'Jon', genrePref: 'rock'},{username: 'Lucy', genrePref: 'pop'},{username: 'Mike', genrePref: 'rock'},{username: 'Luke', genrePref: 'house'},{username: 'James', genrePref: 'house'},{username: 'Dave', genrePref: 'bass'},{username: 'Sarah', genrePref: 'country'},{username: 'Natalie', genrePref: 'bass'}];
 
 var text = "<table border ='1'><tr><th>Username</th><th>genrePref</th></tr>"
 for (var i=0;i<arr.length;i++){
     text+="<tr><td>"+arr[i].username+"</td><td>"+arr[i].genrePref+"</td></tr>"    
 }
 text+="</table>";
    document.write(text);
<html>
<head>
 <title>Javascript</title>
</head>
<body>
</body>
</html>