0

I have this array object that contains arrays:

var webApps = [
    ['leave empty']
];

I'm ajaxing in some content and the end ajax resulting string will be something like this:

ajaxResult = ',["Alex","Somewhere, NY, 11334"],["Zak","Cherryville, FL, 33921"]';

My question is, how do I take the returned string and add it to the webApps array?

halfer
  • 19,824
  • 17
  • 99
  • 186
Damien
  • 4,093
  • 9
  • 39
  • 52

5 Answers5

1

As @Bergi pointed out, it would probably be a good idea to make your ajax call return valid JSON. If thats not something you have control over, then you need to turn it into valid JSON, parse it, and concat to the webApps array:

var webApps = [
    ['leave empty']
];

var ajaxResult = ',["Alex","Somewhere, NY, 11334"],["Zak","Cherryville, FL, 33921"]';

//strip the comma
ajaxResult = ajaxResult.substring(1);

//surround with []
ajaxResult = "[" + ajaxResult + "]";

//parse
ajaxResult = JSON.parse(ajaxResult);

//and concat
webApps = webApps.concat(ajaxResult);
go-oleg
  • 19,272
  • 3
  • 43
  • 44
  • Here's the code i'm using. I've modified it to look like what you're giving me. I actually get an error: object has no substring method. http://jsfiddle.net/grem28/DyJnj/ . All that code i'm using is what i'm using to get it to that ',["alex"]' string. – Damien Aug 01 '13 at 03:04
  • 1
    `$('#ajaxResults').html($('#mArray').text())` will set the contents, not return them. It'll instead return a node list or something like that. – go-oleg Aug 01 '13 at 03:10
  • If the string you shared with us is what is returned in `data`, then just set that to `ajaxResult` and the rest should work. – go-oleg Aug 01 '13 at 03:15
  • the ajaxResult you see isn't how it comes over. That's why i have a bunch of stuff going on so that the final result is the string you see in your example (,["alex"]). In the result i get unordered lists and other html elements. – Damien Aug 01 '13 at 03:23
  • I converted the ajaxResult to a string and i did the JSON.parse(ajaxResult), now a string, and i get the following error: Uncaught SyntaxError: Unexpected token o – Damien Aug 01 '13 at 03:29
  • @Damien: You should read about proper `JSON` syntax here: http://www.json.org/. Then use `console.log` to print out your string contents, figure out what about it is the wrong syntax, and fix it. – go-oleg Aug 01 '13 at 04:23
0

First transform the result into something that is parseable (I hope no other quirks is necessary):

var jsonStr = "["+ajaxResult.slice(1)+"]";
// [["Alex","Somewhere, NY, 11334"],["Zak","Cherryville, FL, 33921"]]
// would be better if it looked like that in the first place

Now we can parse it, and push the single items on your array:

var arr = JSON.parse(jsonStr);
webApps.push.apply(webApps, arr);

We could've used a loop as well, but push can take multiple arguments so it's easier to apply it.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • i was able to get it like this: [["alex","somewhere..."]["zak","cher..."]]. I tried doing something like this: data.replace('][','],[') but that didn't work. – Damien Aug 01 '13 at 03:13
  • I guess it would be better with the comma then. Maybe the replace didn't work because it replaced only one array boundary? Try `data.replace(/\]\[/g, "],[")` instead – Bergi Aug 01 '13 at 03:15
  • I got the replace to work. It now looks like your commented out json string. I'm getting an error when i run it (Uncaught Syntax Error: Unexpected token o). here's the code i'm using: http://jsfiddle.net/grem28/uHT4s/ – Damien Aug 01 '13 at 03:21
  • The `ajaxResult` you're trying to parse there is a jQuery object, not a string!!! (and stringified `"[object Object]"` gives an unexpected token). http://jsfiddle.net/uHT4s/1/ – Bergi Aug 01 '13 at 03:33
  • I actually converted the object to a string: http://jsfiddle.net/grem28/uHT4s/2/ and i'm still getting that error. – Damien Aug 01 '13 at 03:42
  • Yes, exactly that is the problem as I said. `alert()` your string and you'll see. Use [what I suggested](http://jsfiddle.net/uHT4s/1/). – Bergi Aug 01 '13 at 03:45
  • Your suggestion omits the $('#ajaxResults').html($('#mArray').text(). The actual json result is contained in the actual #mArray div... how would i extract that from there without causing a problem? Here's what i have so far, but again, i need to extract from #mArray. Also, the mArray has a (ul) in it, that's why i'm removing it. var ajaxResult = data.replace('][','],['); $('#ajaxResults').html(ajaxResult); $('#ajaxResults ul').remove(); – Damien Aug 01 '13 at 03:54
  • OK, you weren't saying that you're receiving HTML not only that string. Then it should be `$('#ajaxResults').html(data).find("ul").remove(); var ajaxResult = $('#myArray').text(); var jsonStr = "["+ajaxResult.replace('][','],[') + "]"; …` – Bergi Aug 01 '13 at 10:33
  • By the way... thanks for all your help so far!!! greatly appreciated! I tried the code provided and nothing. I console.logged the "var ajaxResult" and i get blank. I also console.log(jsonStr) and nothing either. – Damien Aug 01 '13 at 13:24
  • Please upvote and/or accept one our answers. If you have further problems with ajax and getting the string into `ajaxResult` (as it is your original post), that is beyond the scope of this question. You might consider asking another one, providing more details about the ajax service and the received response. – Bergi Aug 02 '13 at 15:55
0

The following works if the browser supports JSON.

var webApps = [
    ['leave empty'],['one']
];
var str = JSON.stringify(webApps);
// "[["leave empty"],["one"]]"

str = str.substr(0, str.length-1);
//"[["leave empty"],["one"]"

//var arr = eval(str + ajaxResult + "]");
// more secure way
var arr = JSON.parse(str + ajaxResult + "]");
Zafer
  • 2,180
  • 16
  • 28
  • It's faster to use eval but if you want a validation (which is more secure) JSON.parse is more suitable. So there may be a reason :). – Zafer Aug 01 '13 at 03:00
  • Also, this is worth reading: http://stackoverflow.com/questions/1843343/json-parse-vs-eval – Zafer Aug 01 '13 at 03:01
  • 1
    Faster? That must've been decades ago. http://www.google.de/search?q=jsperf+json.parse+eval – Bergi Aug 01 '13 at 03:22
0

webApps = eval("[['"+(webApps[0]).toString()+"']"+ajaxResult+"]");

It's weird, but solve your question.

5z- -
  • 161
  • 4
  • I'm doing this: http://jsfiddle.net/grem28/keg4s/ , I have the ajaxResult looking like this now: [["test","test"],["test","test"]]. I'm getting an error at the eval line: Uncaught SyntaxError: Unexpected identifier – Damien Aug 01 '13 at 03:40
-1

If ajax result is a string, you can convert it to object and add each property to webapps var.

var data = eval('(' + ajaxResult + ')'); // data is a javascript array now, do anything you want
Thanh Khánh
  • 621
  • 1
  • 9
  • 21