猿问

将对象转换成一定格式的对象数组

有这样一个对象

var obj={

a0:0,

a1:1,

a2:2,

...,

an:n,


b0:0,

b1:1,

b2:2,

...,

bn:n,


c0:0,

c1:1,

c2:2,

...,

cn:n

}

我想转换成这样的

var arr=[

{a0:0,b0:0,c0:0},

{a1:1,b1:1,c1:1},

{a2:2,b2:2,c2:2},

...           ,

{an:n,bn:n,cn:n},

]


浮云间
浏览 560回答 1
1回答

白板的微信

var obj={a0:0,a1:1,a2:2,a3:3,b0:0,b1:1,b2:2,b3:3,c0:0,c1:1,c2:2,c3:3};function test (obj, n) {&nbsp; &nbsp; let count = 0;&nbsp; &nbsp; let arr= [];&nbsp; &nbsp; for (key in obj){&nbsp; &nbsp; &nbsp; &nbsp; if (!arr[count]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[count] = { // 第一次执行&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [key]: obj[key]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else { // 已经存在其他元素&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[count][key] = obj[key];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; count < n-1 ? count ++ : count = 0;&nbsp; &nbsp; }&nbsp; &nbsp; console.log(arr);}test(obj, 4)你应该是要酱紫的吧.
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答