[英]How can I loop through an array
I'm having problems with looping through an array.我在遍历数组时遇到问题。 I'm making an RPG bot for Discord, and I need to display the inventory
of the player when prompted.我正在为 Discord 制作一个 RPG 机器人,我需要在出现提示时显示玩家的inventory
。 I've tried to use Object.values()
, Object.map()
and Object.entries()
.我试过使用Object.values()
, Object.map()
和Object.entries()
。 The names of the items
are already worked out with Object.keys(inventory)
, but the values
are the problem.这些items
的名称已经用Object.keys(inventory)
计算出来,但是values
是问题所在。
var invItems = Object.keys(inventory);
var InvValues = Object.entries(inventory);
for (var i = 0; i <= invItems.length; i += 1) {
if (invValues[i[1]] > 0) {
message.channel.send(`${invValues[i[1]]}x ${invItems[i]}`);
}
};
I'm a beginning coder, so please explain more detailed than you usually may do.我是一个初学者,所以请比你通常做的更详细地解释。
You can send values to message.channel
directly using for...of
loop like:您可以使用for...of
循环直接向message.channel
发送值,例如:
for (let [key, value] of Object.entries(inventory)) {
if (value > 0) {
message.channel.send(`${value}x ${key}`);
}
}
Explanation:解释:
The Object.entries()
method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. Object.entries()
方法返回给定对象自己的可枚举字符串键控属性 [key, value] 对的数组。 So, when we use it like:所以,当我们像这样使用它时:
const object1 = { a: 'somestring', b: 42 }; console.log(Object.entries(object1));
You can we get get an array back and each inside array is another array of key-value pair.你可以得到一个数组,每个内部数组都是另一个键值对数组。 Now, we can get each key & value using array destructuring and for...of
loop like:现在,我们可以使用数组解构和for...of
循环来获取每个键和值,例如:
const object1 = { a: 'somestring', b: 42 }; for (let [key, value] of Object.entries(object1)) { console.log(`${key}: ${value}`); } // Returns: // "a: somestring" // "b: 42"
You can see this now returns all the keys and values properly.您现在可以看到这会正确返回所有键和值。 We have to just modify it a little bit to match your requirement and send the required message back.我们只需对其进行一些修改以符合您的要求并发送回所需的消息。
var invItems = Object.keys(inventory);
var invValues = Object.entries(inventory); // fixed the capital i
for (var i = 0; i <= invItems.length; i += 1) {
if (invValues[i] > 0) {
message.channel.send(`${invValues[i]}x ${invItems[i]}`);
}
};
You are not using i
as an index rather you are trying to access i[1]
as if it were an array您没有使用i
作为索引,而是尝试像访问数组一样访问i[1]
also, in Javascript, you can also do a JSON.stringify
to see the object structure.此外,在 Javascript 中,您还可以执行JSON.stringify
以查看 object 结构。 this will print your inventory
to a JSON String
.这会将您的inventory
打印到JSON String
。
//this may help you see what the inventory looks like
for(let i = 0; i<inventory.length; i+=1)
console.log(JSON.stringify(inventory[i]));
You are on the right track with Object.entries
which will provide an array of key/value pair arrays.您在Object.entries
的正确轨道上,它将提供一组键/值对 arrays。 Note that here: invValues[i[1]]
you are trying to access a property 1
of i
with is probably undefined
.请注意,此处: invValues[i[1]]
您试图访问i
的属性1
可能是undefined
。 You can actually avoid some complication if you iterate with forEach
here.如果在此处使用forEach
进行迭代,实际上可以避免一些复杂性。
const inventory = { itemA: 'description for A', itemB: 'description for B' } Object.entries(inventory).forEach(([key, value]) => { // replace with message.channel.send(...) console.log(`${key}: ${value}`) })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.