开发者

Javascript: How can I transform an array?

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 l开发者_JAVA百科ike this:

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

?

Thanks a lot, MEM


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"]


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!


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.


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());


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 ?


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]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜