简体   繁体   English

如何使两个JavaScript数组的项目彼此对应?

[英]How can I make items of two JavaScript arrays correspond to each other?

Delving into arrays today with javascript. 今天就使用javascript研究数组。 I have created two arrays 我创建了两个数组

    var items = ['Bread', 'Milk', 'Butter']
    var calories = [10,20,30]

The idea is that the calories correspond to the item, so bread has 10 calories. 这个想法是卡路里对应于物品,所以面包有10卡路里。 And if I want to see the two arrays combining results then i call this 如果我想查看两个数组结合的结果,那么我称之为

   document.writeln(items[1] +  calories[1])

I'm guessing theres a much more efficient way of doing this, but as a newbie I would appreciate any pointers in the right direction. 我猜想有一种更有效的方法可以做到这一点,但是作为一个新手,我会感激任何朝着正确方向发展的指针。 Ideally I want to store all the info in one array 理想情况下,我想将所有信息存储在一个数组中

One way would be to use an object instead of an array: 一种方法是使用对象而不是数组:

var items = {
   Bread: 10, 
   Milk: 20, 
   Butter: 30
};

alert(items["Bread"]); // Alert 10

There is a good article on MDN about working with objects in JavaScript that might be useful to you. 关于MDN的一篇很好的文章介绍了如何在JavaScript中使用对象,这可能对您有用。

You could use an array of objects: 您可以使用对象数组:

var items = [
   {Bread: 10},
   {Milk:20}
];

or simply an object 或仅仅是一个对象

var items {
    Bread:10,
    Milk:20
}

To print the breads calories using a Key-Value approach: 要使用键值方法打印面包的卡路里,请执行以下操作:

   alert(items["Bread"]);

If you dont want to use the itemname as the "key", you can do this: 如果您不想将项目名称用作“键”,则可以执行以下操作:

var items = [
   {
    Item: "Bread", 
    Calories: 10
   },
   {
    Item: "Milk", 
    Calories: 20
   }
];

Then you can print it like so: 然后,您可以像这样打印它:

alert(items[0].Item+" has "+items[0].Calories+" calories");

Then, if you wish to add elements: 然后,如果您想添加元素:

items.push({Item:"Butter",Calories:30});

Not really sure what you mean by "more efficent" really but something like this is more like you want to model: 不太确定“提高效率”的意思是什么,但是类似这样的事情更像是您要建模的事情:

var items = {
  'Bread': 10,
  'Milk': 20,
  'Butter': 30
};

How about hashtables/dictionaries? 哈希表/字典怎么样?

var calories = { Bread: 10,  Milk: 20, Butter: 30 };

To get the desired output from this object, check my (Elias Van Ootegem) answer. 要从该对象获得所需的输出,请检查我的(Elias Van Ootegem)答案。
or "objects": 或“对象”:

var food = [ 
     { name: 'Bread', calories: 10 },
     { name: 'Milk', calories: 20 },
     { name: 'Butter', calories: 30 },
];

To get the output here: 要在此处获得输出:

for (var i=0;i<food.length;i++)
{
    document.writeln(food[i].name +': '+food[i].calories);
}

You're probably looking for an object literal. 您可能正在寻找对象文字。 You want your output to be something like this: 您希望您的输出是这样的:

var items = {
    Bread: 10,
    Milk : 20
};

You can loop through the array and fill an initially empty array, like this: 您可以遍历数组并填充最初为空的数组,如下所示:

var items = {}, food, calories;

food = ['Bread', 'Milk', 'Butter'];
calories = [10,20,30];

for (var i = 0; i < food.length; i++) {
    for (var l = 0; l < calories.length; l++) {
        items[ food[i] ] = calories[ l ];
    }
}

Yes, so like everybody tells you, an object is what you need. 是的,所以就像每个人都告诉您的那样,您需要一个对象。 To get to the actual output: 要获得实际输出:

var items = {
   'Bread' : 10, 
   'Milk', : 20, 
   'Butter' : 30
};
for (var name in items)
{//loop through object literal, list all properties
    if (items.hasOwnProperty(name))
    {//avoid output of prototype properties
        document.writeln(name + ': ' + items[name]);
    }
}
//output:
    Bread: 10
    Milk: 20
    Butter: 30

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

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