最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript: push array values from string variable - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Oct 15, 2013 at 20:03 Jonny07Jonny07 6253 silver badges14 bronze badges 3
  • 5 When it's "inside" a string, it's just a string. It would be pletely crazy for JavaScript to peek inside every string to find out whether there's a way to parse it. In your case, you don't even have a valid JSON structure. – Pointy Commented Oct 15, 2013 at 20:05
  • Is there any way I can achieve this manually? – Jonny07 Commented Oct 15, 2013 at 20:07
  • The best thing would be to make sure the server side prepares a clean JSON structure for you. – Pointy Commented Oct 15, 2013 at 20:12
Add a ment  | 

3 Answers 3

Reset to default 7

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 ments, 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.

Two possibilities:

JSON

Make the server send JSON instead of an arbitrary data format so you have the following:

var response = "[[0,1],[1,5],[2,3],[3,10]]"; // JSON data ing as string from somewhere
Multi = JSON.parse(response);

(Documentation of JSON.parse())

String.split()

Split the string at pattern instances, if you only ever receive arrays of arrays of numbers or other nicely behaving data:

var response = "[0,1],[1,5],[2,3],[3,10]"; // string ing from somewhere
Multi = response.split(/\s*\]\s*,\s*\[\s*/).map(
    function(s) {
        return s.split(/\s*,\s*/).map(parseInt);
    });

If you have control of the AJAX input that you're reading, you could try:

var Multi = [],
    test = "0,1|1,5|2,3|3,10",
    groups = test.split('|');

for (var i = 0; i < groups.length; i++) {
    var tmpArray = groups[i].split(',');
    Multi.push(tmpArray);
}
发布评论

评论列表(0)

  1. 暂无评论