Cast javascript object to array. How to?

前端 未结 6 1529
有刺的猬
有刺的猬 2020-12-08 20:47

I have made this sandbox test:


    
        whatever
        

        
相关标签:
6条回答
  • 2020-12-08 21:24

    Use a for loop for max browser compatibility.

    In Javascript all arrays are objects, but not all object are arrays. Take a look at this Perfection Kills page which describes how to check that something is an Array.

    To check for an array, you can use Object.prototype.toString.call(theObject). This will return [object Array] for an object that is an Array and [object Object] for an object that's not an Array (see example below):

                function myLittleTest() 
                {
                    var obj, arr, armap, i;    
    
                      // arr is an object and an array
                    arr = [1, 2, 3, 5, 7, 11]; 
    
                    obj = {}; // obj is only an object... not an array
    
                    alert (Object.prototype.toString.call(obj));
                      // ^ Output: [object Object]
    
                    obj = arr; // obj is now an array and an object
    
                    alert (Object.prototype.toString.call(arr));
                    alert (Object.prototype.toString.call(obj));
                      // ^ Output for both: [object Array]
    
                    // works in IE
                    armap = [];
                    for(i = 0; i < obj.length; ++i)
                    {
                        armap.push(obj[i] * obj[i]);
                    }
    
                    alert (armap.join(", ")); 
    
                }
                // Changed from prueba();
                myLittleTest();
    

    jsFiddle example

    0 讨论(0)
  • 2020-12-08 21:27

    Among many other small utilities for manipulating objects and arrays, Underscore.js offers a toArray(obj) helper method. Documentation here: http://underscorejs.org/#toArray

    It's not totally obvious from the way the documentation is written, but it works like a charm on arbitrary objects. When given an object, it iterates over the values and returns a list that contains just those values.

    0 讨论(0)
  • 2020-12-08 21:31

    If you have an array-like object, (like arguments, for example,) you can get a real array made from it by calling Array.prototype.slice.call(o).

    var o = {0:"a", 1:'b', length:2};
    var a = Array.prototype.slice.call(o);
    

    a will be ["a", "b"]. This will only work right if you have a correctly set length property.

    0 讨论(0)
  • 2020-12-08 21:33

    I'm pretty sure this isn't a type problem, it's because IE didn't have the Array.map() function until IE 9. See http://msdn.microsoft.com/en-us/library/k4h76zbx(v=VS.85).aspx for a list of supported functions. See http://msdn.microsoft.com/en-us/library/ff679976(v=VS.94).aspx for a description of the Array.map() function in IE 9.

    0 讨论(0)
  • 2020-12-08 21:34

    A year ago now, but I may as well mention jQuery's makeArray function http://api.jquery.com/jQuery.makeArray/

    0 讨论(0)
  • 2020-12-08 21:44

    I think you are trying too hard...

    It's easiest with jQuery (or similar library)

    For this object:

    var obj = {a: 1, b: 2, c: 3};
    

    Arrays have a fixed key system so for the object above, you've got to throw away either the keys (a, b, c) or the values (1, 2, 3)

    So either this:

    var arr = $.map(obj, function (value, key) { return value; });
    

    or this:

    var arr = $.map(obj, function (value, key) { return key; });
    
    0 讨论(0)
提交回复
热议问题