[removed] How can I transform an array?

前端 未结 6 902
-上瘾入骨i
-上瘾入骨i 2021-01-12 05:10

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

相关标签:
6条回答
  • 2021-01-12 05:26
    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());
    
    0 讨论(0)
  • 2021-01-12 05:26

    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 ?

    0 讨论(0)
  • 2021-01-12 05:27

    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"]
    
    0 讨论(0)
  • 2021-01-12 05:41

    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 讨论(0)
  • 2021-01-12 05:42

    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!

    0 讨论(0)
  • 2021-01-12 05:42

    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.

    0 讨论(0)
提交回复
热议问题