views:

87

answers:

5

I have this on a javascript var: (it's a http returned data, and I don't know if it's an array or string - (how can we see that?) - Update: using typeof returned "string", so it's a string.

[{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}]

How can we pass/transform that, into something like this:

["gggg.fa","rarar.fa"]

?

Thanks a lot, MEM

A: 

it's a http returned data, and I don't know if it's an array or string

It's JSON, and you can use it directly in JavaScript.

If you transform it into your array, you will lose the association key / value ; are you sure it's what you want ?

Guillaume Lebourgeois
Not sure really. I'm loosing all my faith here. The point is to pass this data to a autocomplete plugin and, I don't understand what that plugin wants. I'm completely lost here. It's related: http://stackoverflow.com/questions/3609581/jquery-autocomplete-help-with-code-what-nextX.X
MEM
+1  A: 

This question is strongly related with this one.

I would suggest reading my answer there, as it would really help; and with a little variation, it would just work:

var responseString = '[{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}]',
    responseObject = JSON.parse(responseString),
    nombresDeDominio = [];

for(var i in responseObject) {
  nombresDeDominio.push(responseObject[i].nomeDominio)
}

Suerte!

alcuadrado
+1  A: 

Assuming your data always looks like that, you can do something like this:

var foo = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}];
var newarr = [];
for ( var i=0,j=foo.length;i<j;i++ ) {
    newarr.push( foo[i]['nomeDominio'] );
}

Here's a working fiddle.

Ken Redler
Thanks a lot. I really must see much more for in order to use them when I need one. We may not know if a language as a given sintax keyword but, for is almost certain. Thanks a lot for exemplifying with one.
MEM
A: 

Okay, firstly to get the type of a "thing", use the "typeof" operator (note that the type of an array is an object, not 'array'!):

var a = "string";
var b = 1;
var c = new Array();
alert(typeof(a)); // string
alert(typeof(b)); // number
alert(typeof(c)); // object

To get at the values in the associative array (assuming it is one), you can just loop through it, like so:

var d = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}];
d["bob"] = "alice";
d["gary"] = "stephen";

for(var key in d) {
    alert(d[key]);
}
Stephen
Thanks. I've used typeof and I will try not to forget that important note about object been an array. ;) Thanks a lot.
MEM
+3  A: 

You can figure out if is a string or an already parsed object by checking the type of your variable, e.g.:

ajax('url', function (response) {
  alert(typeof response);
});

You will now figure out if it's a "string" or an Array "object".

If it's a string, you can use the JSON.parse method as @alcuadrado suggest, otherwise you can simply use the array.

Several answers suggest the use of the for-in statement to iterate over the array elements, I would discourage you to use it for that.

The for-in statement should be used to enumerate over object properties, to iterate over Arrays or Array-like objects, use a sequential loop as @Ken Redler suggests.

You should really avoid for-in for this purpose because:

  • The order of enumeration is not guaranteed, properties may not be visited in the numeric order.
  • Enumerates also inherited properties.

You can also use the Array.prototype.map method to meet your requirements:

var response = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}];
var array = response.map(function (item) { return item.nomeDominio; });
// ["gggg.fa", "rarar.fa"]
CMS
Great answer bro, I didn't know the difference of sequential loop and for-in in arrays.The first snippet may look a little confusing though, whats that ajax function? And how can a response give you a parsed array? Thanks (:
alcuadrado
+1 for `map()`. Sadly, you have to make your own (conditional) implementation if you want IE7/8 along for the ride. IE9 has it, though.
Ken Redler
Thanks a lot CMS for your great comment and detail. I have used alcuadrado suggestion because it was a string, after using typeof as suggested.
MEM
@alcuadrado: You're welcome, the `ajax` function is just a library-agnostic example of an Ajax request, which the OP presumably does.
CMS
@Ken: Yeah, IE9 is getting pretty good!, the Sept. 15 they will release another Platform Preview Release :)
CMS
@CMS -- Sweet. Just a few more years to go and we'll all be on the same page!
Ken Redler