Javascript: How to convert an array?

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

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

How can we pass / convert this to something like this:

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

?

Thanks a lot, MEM

+7
javascript arrays
source share
6 answers

This question is strongly related to this .

I would suggest reading my answer , as that would really help; and with a little change it just works:

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

Suerte!

+7
source share

You can determine if a string or an object has already been processed by checking the type of your variable, for example:

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

Now you will find out if it is a "string" or an array of "object" .

If it is a string, you can use the JSON.parse method, as @alcuadrado suggests, otherwise you can just use an array.

Several answers suggest using the for-in statement to iterate over the elements of an array, I would discourage you from using it for this.

The for-in operator should be used to list the properties of an object, to iterate over arrays or objects like Array, use a sequential loop, as @Ken Redler suggests.

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

  • The order of listing is not guaranteed; properties cannot be visited in numerical order.
  • Enumerates also inherited properties.

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

 var response = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}]; var array = response.map(function (item) { return item.nomeDominio; }); // ["gggg.fa", "rarar.fa"] 
+9
source share

Assuming your data always looks like this, 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'] ); } 

This is where fiddle works.

+1
source share
 function transform(array, f) { var ret = []; $.each(array, function(index) { var v = f.call(this, index); if(v) { ret.push(v); } }); return ret; } var result = transform( [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}], function() { return this.nomeDominio; } ); alert(result.toString()); 
+1
source share

this is http data and i don't know if it is an array or string

This is JSON , and you can use it directly in JavaScript.

If you convert it to your array, you will lose the key / value of the association; Are you sure you want?

0
source share

Well, firstly, to get the type โ€œthingโ€, use the operator โ€œtypeofโ€ (note that the type of the array is an object , not an โ€œ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 the values โ€‹โ€‹in an associative array (suppose it's one), you can simply skip it, for example:

 var d = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}]; d["bob"] = "alice"; d["gary"] = "stephen"; for(var key in d) { alert(d[key]); } 
0
source share

All Articles