0

I have an initial array (users) with multiple (string and numeric) arrays therein:

var users = [
['User: 10792 - Jack',45.7546,-117.807,2,'/res/smR11.gif'], ['User: 11248 - John',38.0867,131.976,3,'/res/smR08.gif']
];

I have a string of data from our server in the current form of:

newData = "['User: 18469 - Gary',-33.9399732539481,151.164383805489,3,'/res/markerw.gif'],['User: 10020 - Robert',40.6437563454472,-73.7593346140851,6,'/res/smR10.gif']";

I erase all existing data with users.length = 0;

I then need to insert the newData into the users array.

NOTE: I can obviously modify the server data into any other format that would be more suitable.

Any help would be greatly appreciated.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • What is your input and expected output? – Halcyon Oct 25 '13 at 11:04
  • You give no enough information. What the language and techonlogy do you use? ASP.NET MVC / Ruby on rails, PHP, etc – alexmac Oct 25 '13 at 11:04
  • 3
    [JSON](http://json.org/) would be more suitable. It then would be as as simple as `users = JSON.parse(newData)` (overwriting, not appending) – Bergi Oct 25 '13 at 11:04

2 Answers2

2

try something like this

     var users = JSON.parse(newData);
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
-1

Your newData string looks very similar to the javascript above it. How about this...

users = eval('[' + newData + ']');

[EDIT] As Bergi, rajeshkakawat and StephenJames pointed out, eval will work but is less secure.

See: JSON.parse vs. eval()

Community
  • 1
  • 1
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • this would work, but use of eval is generally discouraged because it isn't that safe and is also sluggish, `JSON.parse` would be a more efficient and robust method. – Stephen James Oct 25 '13 at 11:13
  • @user2919549 Given it was actually Stephen's idea, maybe you could upvote one of his answers – jozxyqk Oct 25 '13 at 11:49