最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - JavaScript, transform object into array - Stack Overflow

matteradmin15PV0评论

I've got an object:

var obj = {
    "Mike": 24,
    "Peter": 23,
    "Simon": 33,
    "Tom": 12,
    "Frank": 31
};

I want to create an array that holds the values of the object. The keys (key names) can be disregarded:

[24, 23, 33, 12, 31]

The order of the values is NOT important!

One solution (obviously) would be do have a function that takes the values and puts them into an array:

var arr = valuesToArray(obj); 

I will accept such a function as the answer. However, I would be more pleased if there would be an API function (ECMAScript, jQuery, browser-specific, ...) that could do this. Is there such a thing?

I've got an object:

var obj = {
    "Mike": 24,
    "Peter": 23,
    "Simon": 33,
    "Tom": 12,
    "Frank": 31
};

I want to create an array that holds the values of the object. The keys (key names) can be disregarded:

[24, 23, 33, 12, 31]

The order of the values is NOT important!

One solution (obviously) would be do have a function that takes the values and puts them into an array:

var arr = valuesToArray(obj); 

I will accept such a function as the answer. However, I would be more pleased if there would be an API function (ECMAScript, jQuery, browser-specific, ...) that could do this. Is there such a thing?

Share Improve this question asked Jan 5, 2011 at 18:48 Šime VidasŠime Vidas 186k65 gold badges286 silver badges391 bronze badges
Add a comment  | 

9 Answers 9

Reset to default 47

The obvious way would be to do a for-in loop, as @quixoto suggests, but just for the record, and since you are looking for a built-in way, you could pair the new ECMAScript 5 methods Object.keys and Array.prototype.map, available on latest browsers:

function valuesToArray(obj) {
  return Object.keys(obj).map(function (key) { return obj[key]; });
}

UPDATE: ES2017 introduced the Object.values method, which does exactly what you want.

Additionally, ES2017 adds another often useful method, Object.entries. This method returns an array of key-value pairs.

const obj = {
    "Mike": 24,
    "Peter": 23,
    "Simon": 33,
    "Tom": 12,
    "Frank": 31
};

const values = Object.values(obj);
const entries = Object.entries(obj);
console.log('values:', values);
console.log('entries:', entries);

Use Object.values it will return array.

Object.values(obj) // [24, 23, 33, 12, 31]

There's no built-in way to do this anywhere. The following does what you suggest, and may be "shortened" into more clever functional-programming versions depending on your library, but they'll all have the same efficiency.

function valuesToArray(obj) {
    var result = [];
    for (var key in obj) {
       if (obj.hasOwnProperty(key)) {
           result.push(obj[key]);
       }
    }
    return result;
}

With jQuery you could use the each function:

var obj = {
    "Mike": 24,
    "Peter": 23,
    "Simon": 33,
    "Tom": 12,
    "Frank": 31
}

myArray=new Array();
$.each(obj, function(key, value) { 
  myArray.push(value);
});

Posting this strictly for fun. Save your downvotes. I'm not recommending it actually be used. ;o)

Example: http://jsfiddle.net/WGpXX/

var arr = eval( '[' +
    JSON.stringify(obj)
    .slice(1,-1)
    .replace(/"[^"]+":/g,'')
    + ']');

Technically works in this simple case.

Using the Underscore lib try:

function valuesToArray(o) {
    return _.pairs(o);
}

var obj = {
    "Mike": 24,
    "Peter": 23
    //...
    },
    result = valuesToArray(obj);

Then the result is [ ["Mike", 24], ["Peter", 23] ];

More detail on the pairs method here: http://underscorejs.org/#pairs

Try this:

var obj = {     "Mike": 24,     "Peter": 23,     "Simon": 33,     "Tom": 12,     "Frank": 31 } ;
    var arr = []
    for(var a in obj)
    {
        var val = obj[a];
        arr.push(val);
    }
alert(arr.length)

Some libraries have something to do this (such as prototype's "values" function), but they're really just wrappers around a function that loops over and returns the values of the object.

http://www.prototypejs.org/api/object/values

Using bob.js this can be done pretty simply:

function valuesToArray(obj) {
    return bob.collections.extensions.toArray.call(obj);
}
Post a comment

comment list (0)

  1. No comments so far