2

Hello I have a javascript question whereby I am getting a JSON result:

{"0":"San Diego acls","1":"San Diego pals","2":" San Diego CPR","3":" Temecula acls","4":" Temecula pals"}

which is stored in a variable called data.

I want to parse this data variable and make a list like:

San Diego acls, San Diego pals, San Diego CPR, Temecula acls, Temecula pals

Any elegant ways?

Thanks

Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169
  • 2
    `var values = {"0":"San Diego acls","1":"San Diego pals","2":" San Diego CPR","3":" Temecula acls","4":" Temecula pals"}` you don't need to parse it you can access it like array already `values[0], values[1]` this is because you have the appropriate keys and JS will properly convert them ... when doing the comparison. – Samy Vilar Jun 16 '12 at 05:33

3 Answers3

5

What you need is this:

var res = [];
for (var x  in obj)
    if (obj.hasOwnProperty(x))
        res.push(obj[x]);
console.log(res.join(","));

And there's one more way of 'elegantly' doing it (taken from Alex's answer),

res = [];
Object.keys(obj).forEach(function(key) {
    res.push(obj[key]);
});
console.log(res.join(","));

In case you need the result in that specific order, sorting the keys (that come from Object.keys(obj)) before invoking forEach on the array will help. Something like this:

Object.keys(obj).sort().forEach(function(key) {
Community
  • 1
  • 1
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
  • This is not guaranteed to yield the list in the same order as the keys; i.e. item 0 may come last, item 1 might come first, etc. – Asherah Jun 16 '12 at 06:08
  • @Len: Its an object. There's no specific order. If you want one, do a sort on `Object.keys(obj)` before you invoke `forEach` on it. – UltraInstinct Jun 16 '12 at 06:11
  • @Thrustmaster: clearly, but the OP wants them in the order the object has according to the keys. Given the keys are `0`, `1`, `2` …, and OP's example shows the output with order accordingly, that would be obvious I'd think. It just looks like someone's poorly serialised an array at the other end, and now OP wants an array again. – Asherah Jun 16 '12 at 06:13
  • @Len: I have updated the answer with what I was talking about. If the OP wants the data to appear in that exact order, he should actually have an array of objects, not objects which he wants to treat like an array. Objects intrinsically dont have any order. A custom sorting is the best OP can get. – UltraInstinct Jun 16 '12 at 06:22
  • @Thrustmaster: I am well aware of that; just trying to convey what appears to be what OP's after. The question is "given this object with numeric keys, I want this output", and the output is in the order of those keys. Saying "he should actually have an array" is pretty useless because s/he *doesn't*; that's a different question. – Asherah Jun 16 '12 at 07:43
  • Its not useless. Its a suggestion -- a better approach that makes **sense**. What I am saying is: there's no point asking for something which the language doesn't guarantee; if you want that something, change your code. BTW, can we stop here? Its, IMO, not helping anyone. – UltraInstinct Jun 16 '12 at 08:22
0

This is very simple in Javascript. You can access the variable data like this:

alert(data[0]);

which should alert "San Diego acls". Do the same for all of them using a loop. You can concatenate strings easily with the + operator.

var result = "";
for (var dString in data) { 
    result += dString + ", ";
}

This will create a string called result and add the elements of the strings in the array to it. It will also add ", " between each element as you described in the question.

  • I did that but the loop never plays. The length doesnt seem to be working. I can access the elements like data[0] however. – Strong Like Bull Jun 16 '12 at 05:54
  • I don't know when you tried it, but when I first answered, I made a mistake with the length part. It should be `data.length` as it is now. Is that what you're trying? –  Jun 16 '12 at 05:57
  • @jini: Its because its an object, not an array. – UltraInstinct Jun 16 '12 at 05:59
  • @jini Ah. Yes the loop would work for arrays. Try Thrustmaster's loop in his answer. –  Jun 16 '12 at 06:02
0

This one also works.

$(document).ready(function(){
    var data = {"0":"San Diego acls","1":"San Diego pals","2":" San Diego CPR","3":" Temecula acls","4":" Temecula pals"};
    var csv = $.map(data,function(data){ return data;});
    alert(csv);
});

jsfiddle DEMO

Bob
  • 1,489
  • 1
  • 16
  • 28