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.6k16 gold badges61 silver badges75 bronze badges
asked Oct 25, 2013 at 11:02
3
  • What is your input and expected output? Commented Oct 25, 2013 at 11:04
  • You give no enough information. What the language and techonlogy do you use? ASP.NET MVC / Ruby on rails, PHP, etc Commented Oct 25, 2013 at 11:04
  • 3
    JSON would be more suitable. It then would be as as simple as users = JSON.parse(newData) (overwriting, not appending) Commented Oct 25, 2013 at 11:04

2 Answers 2

2

try something like this

 var users = JSON.parse(newData);
answered Oct 25, 2013 at 11:12
Sign up to request clarification or add additional context in comments.

Comments

-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()

answered Oct 25, 2013 at 11:08

2 Comments

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.
@user2919549 Given it was actually Stephen's idea, maybe you could upvote one of his answers

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.