Javascript 使用 jQuery 将 JS 对象转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6857468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a JS object to an array using jQuery
提问by The Bndr
My application creates a JavaScript object, like the following:
我的应用程序创建了一个 JavaScript 对象,如下所示:
myObj= {1:[Array-Data], 2:[Array-Data]}
But I need this object as an array.
但我需要这个对象作为一个数组。
array[1]:[Array-Data]
array[2]:[Array-Data]
So I tried to convert this object to an array by iterating with $.each
through the object and adding the element to an array:
所以我尝试通过遍历$.each
对象并将元素添加到数组来将此对象转换为数组:
x=[]
$.each(myObj, function(i,n) {
x.push(n);});
Is there an better way to convert an object to an array or maybe a function?
有没有更好的方法将对象转换为数组或函数?
回答by Joel Richard
If you are looking for a functional approach:
如果您正在寻找一种功能方法:
var obj = {1: 11, 2: 22};
var arr = Object.keys(obj).map(function (key) { return obj[key]; });
Results in:
结果是:
[11, 22]
The same with an ES6 arrow function:
与 ES6 箭头函数相同:
Object.keys(obj).map(key => obj[key])
With ES7 you will be able to use Object.values
instead (more information):
使用 ES7,您将能够使用Object.values
(更多信息):
var arr = Object.values(obj);
Or if you are already using Underscore/Lo-Dash:
或者,如果您已经在使用 Underscore/Lo-Dash:
var arr = _.values(obj)
回答by Dogbert
var myObj = {
1: [1, 2, 3],
2: [4, 5, 6]
};
var array = $.map(myObj, function(value, index) {
return [value];
});
console.log(array);
Output:
输出:
[[1, 2, 3], [4, 5, 6]]
回答by Nicola Peluchetti
I think you can use for in
but checking if the property is not inerithed
我认为您可以使用for in
但检查该财产是否未继承
myObj= {1:[Array-Data], 2:[Array-Data]}
var arr =[];
for( var i in myObj ) {
if (myObj.hasOwnProperty(i)){
arr.push(myObj[i]);
}
}
EDIT - if you want you could also keep the indexes of your object, but you have to check if they are numeric (and you get undefined values for missing indexes:
编辑 - 如果你想你也可以保留你的对象的索引,但你必须检查它们是否是数字(并且你会得到缺失索引的未定义值:
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
myObj= {1:[1,2], 2:[3,4]}
var arr =[];
for( var i in myObj ) {
if (myObj.hasOwnProperty(i)){
if (isNumber(i)){
arr[i] = myObj[i];
}else{
arr.push(myObj[i]);
}
}
}
回答by Pila
Simply do
简单地做
Object.values(obj);
That's all!
就这样!
回答by bjornd
If you know the maximum index in you object you can do the following:
如果您知道对象中的最大索引,则可以执行以下操作:
var myObj = {
1: ['c', 'd'],
2: ['a', 'b']
},
myArr;
myObj.length = 3; //max index + 1
myArr = Array.prototype.slice.apply(myObj);
console.log(myArr); //[undefined, ['c', 'd'], ['a', 'b']]
回答by Maxdow
Since ES5 Object.keys() returns an array containing the properties defined directly on an object (excluding properties defined in the prototype chain):
由于 ES5 Object.keys() 返回一个包含直接定义在对象上的属性的数组(不包括在原型链中定义的属性):
Object.keys(yourObject).map(function(key){ return yourObject[key] });
ES6 takes it one step further with arrow functions:
ES6 使用箭头函数更进一步:
Object.keys(yourObject).map(key => yourObject[key]);
回答by Stopi
Nowadays, there is a simple way to do this : Object.values().
现在,有一种简单的方法可以做到这一点:Object.values()。
var myObj = {
1: [1, 2, 3],
2: [4, 5, 6]
};
console.log(Object.values(myObj));
Output:
输出:
[[1, 2, 3], [4, 5, 6]]
This doesn't required jQuery, it's been defined in ECMAScript 2017.
It's supported by every modern browser (forget IE).
这不需要 jQuery,它已在 ECMAScript 2017 中定义。
每个现代浏览器都支持它(忘记 IE)。
回答by Friedrich
The best method would be using a javascript -only function:
最好的方法是使用 javascript -only 函数:
var myArr = Array.prototype.slice.call(myObj, 0);
回答by Aditya Singh
ECMASCRIPT 5:
ECMASCRIPT 5:
Object.keys(myObj).map(function(x) { return myObj[x]; })
ECMASCRIPT 2015 or ES6:
ECMASCRIPT 2015 或 ES6:
Object.keys(myObj).map(x => myObj[x])
回答by nobody
x = [];
for( var i in myObj ) {
x[i] = myObj[i];
}