javascript: push array values from string variable

Jonny07

So as the title says, I'm trying to set an array from a string variable.

/*** THIS DOES NOT WORK ***/
var Multi = [];
var test = "[0,1],[1,5],[2,3],[3,10]";
Multi.push(test);


/*** THIS WORKS ***/
var Multi = [];
Multi.push([0,1],[1,5],[2,3],[3,10]);

Why is it that when I save it as a string it doesn't work? I need for it to work in a string as I'm pulling these values from another PHP file via AJAX.

Thanks.

Andrew Clark

You can convert your string to valid JSON by adding [ to the front and ] to the back, and then use JSON.parse() to convert your JSON string to an array. Now that you have an array from you string you can extend the Multi array using Multi.push.apply(Multi, array) as shown below:

var Multi = [];
var test = "[0,1],[1,5],[2,3],[3,10]";
Multi.push.apply(Multi, JSON.parse('[' + test + ']'));

As mentioned in comments, a cleaner approach would be to make sure that your PHP file generates valid JSON. All of this answer would be the same except that you wouldn't need to manually add the square brackets.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

push javascript variable into array

From Dev

mongodb Push values into array variable

From Dev

Get id values from child inputs and push it to an array in Javascript on change

From Dev

(PHP) Array push string with variable

From Dev

how to push the array values in javascript

From Dev

Javascript: push array into array vs. push values into array?

From Dev

How to convert string values from variable to HTML in Javascript

From Dev

How to push values from 1 dimensional array to 2 dimensional array in Javascript?

From Dev

How to push an array of values and keys to a JavaScript Object?

From Dev

Sum Array push values, before a 0 javascript

From Dev

JavaScript: Push Array with multiple values on same index

From Dev

Push two array with key values in Javascript

From Dev

how to add array values in javascript using push

From Dev

Push multiple values into an array in Javascript not working

From Dev

angularjs - javascript - Convert object values from array to string

From Dev

angularjs - javascript - Convert object values from string to array structure

From Dev

Get variable from query string, match it to an array in Javascript

From Dev

how to push values from listObjects to an array

From Dev

how to push values from listObjects to an array

From Dev

Extract variable values from String

From Dev

javascript .push overwrites previous array values by duplicating the new values

From Dev

Parsing values from string array

From Dev

Building array from string values

From Dev

Parsing values from string array

From Dev

Paste values from array into string

From Dev

Replace string to values from array

From Dev

How to push elements in JSON from javascript array

From Dev

How to push elements in JSON from javascript array

From Dev

JavaScript. Push and read from Multidimensional Array

Related Related

  1. 1

    push javascript variable into array

  2. 2

    mongodb Push values into array variable

  3. 3

    Get id values from child inputs and push it to an array in Javascript on change

  4. 4

    (PHP) Array push string with variable

  5. 5

    how to push the array values in javascript

  6. 6

    Javascript: push array into array vs. push values into array?

  7. 7

    How to convert string values from variable to HTML in Javascript

  8. 8

    How to push values from 1 dimensional array to 2 dimensional array in Javascript?

  9. 9

    How to push an array of values and keys to a JavaScript Object?

  10. 10

    Sum Array push values, before a 0 javascript

  11. 11

    JavaScript: Push Array with multiple values on same index

  12. 12

    Push two array with key values in Javascript

  13. 13

    how to add array values in javascript using push

  14. 14

    Push multiple values into an array in Javascript not working

  15. 15

    angularjs - javascript - Convert object values from array to string

  16. 16

    angularjs - javascript - Convert object values from string to array structure

  17. 17

    Get variable from query string, match it to an array in Javascript

  18. 18

    how to push values from listObjects to an array

  19. 19

    how to push values from listObjects to an array

  20. 20

    Extract variable values from String

  21. 21

    javascript .push overwrites previous array values by duplicating the new values

  22. 22

    Parsing values from string array

  23. 23

    Building array from string values

  24. 24

    Parsing values from string array

  25. 25

    Paste values from array into string

  26. 26

    Replace string to values from array

  27. 27

    How to push elements in JSON from javascript array

  28. 28

    How to push elements in JSON from javascript array

  29. 29

    JavaScript. Push and read from Multidimensional Array

HotTag

Archive