简体   繁体   中英

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. I've tried to use Object.values() , Object.map() and Object.entries() . The names of the items are already worked out with Object.keys(inventory) , but the values are the problem.

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 (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. 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:

 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

also, in Javascript, you can also do a JSON.stringify to see the object structure. this will print your inventory to a 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. Note that here: invValues[i[1]] you are trying to access a property 1 of i with is probably undefined . You can actually avoid some complication if you iterate with forEach here.

 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}`) })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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