[英]Populate array from an object array in javascript
I've got the following object that's an object array (an array of objects), on a variable: 我在变量上有以下对象,它是对象数组(对象数组):
variable: myVar1 变量:myVar1
On fireBug I get the following lines when I call myVar1: 在fireBug上,当我调用myVar1时,我得到以下行:
[Object { myId= "1", myName= "Name1" }, Object { myId= "2", myName= "Name2" }, Object { myId= "3", myName= "Name3" }]
I need this data to be in the followng format stored on a variable too: 我需要这些数据也是以变量形式存储在变量中:
myVar2 = [
[1, 'Name1'],
[2, 'Name2'],
[3, 'Name3']
]
I've tried so many things like the use of for loops and js functions but can't get it work. 我已经尝试了很多事情,例如使用for循环和js函数,但是无法正常工作。 I supose it's so easy.
我觉得这很容易。 Anyone could try to explain the procedure.
任何人都可以尝试解释这个程序。
Thank you 谢谢
Solution for browsers which support Array.map()
: 支持
Array.map()
浏览器的解决方案:
var result = arr.map(function(o) { return [+o.myId, o.myName]; });
For compatibility you may use the shim provided at MDN . 为了兼容性,您可以使用MDN提供的垫片 。
The following should do the trick: 以下应该做的伎俩:
var myVar2 = [];
for (var i = 0; i < myVar1; i++) {
myVar2.push([myVar1[i].myId, myVar1[i].myName]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.