Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JavaScript updating an array element with index

JavaScript updating an array element with index

Problem

I currently have an arrayList() called players in JavaScript in which I push data into it using: players.push({name: msg.name, map: msg.map, coords: msg.coords});

Here is an example data: { name: 'weka', map: '5', coords: '12.4' } for example players[0].

Here is what I am doing:

for (var i = 0; i 
Problem courtesy of: nn2

Solution

players[i].push({name: msg.name, map: msg.map, coords: msg.coords});

should be

players[i] = {name: msg.name, map: msg.map, coords: msg.coords});

You haven't defined players[i] to be an Array, so there's nothing to "push" to. And I asume that you wish to change existing value and not insert an extra depth.

Solution courtesy of: Robin Castlin

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

JavaScript updating an array element with index

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×