我有一个对象数组的列表
var list = {
Achievement: ["110", "100", "104", "110"],
Emp Code : ["1000001", "1000001", "1000001", "1000001"],
Product :["Product A ", "Product B", "Product A ", "Product B"],
Reportee Name :["Harry", "Harry", "Peter", "Peter"],
Target : ["116", "94", "105", "114"],
percentage: ["94.82758621", "106.3829787", "99.04761905", "96.49122807"]
}
和我想要的数组在下面提到。关键项应该是数组的一部分。
var list = {
0: ["Achievement","110", "100", "104", "110"],
1 : ["Emp Code","1000001", "1000001", "1000001", "1000001"],
2 :["Product" ,"Product A ", "Product B", "Product A ", "Product B"],
3 :["Reportee Name","Harry", "Harry", "Peter", "Peter"],
4 : ["Target","116", "94", "105", "114"],
5: ["percentage","94.82758621", "106.3829787", "99.04761905", "96.49122807"]
}
答案 0 :(得分:2)
您可以使用Object.keys()
和.reduce()
方法:
let data = {'Achievement': ["110", "100", "104", "110"],'Emp Code' : ["1000001", "1000001", "1000001", "1000001"],'Product' :["Product A ", "Product B", "Product A ", "Product B"],'Reportee Name' :["Harry", "Harry", "Peter", "Peter"],'Target' : ["116", "94", "105", "114"],'percentage': ["94.82758621", "106.3829787", "99.04761905", "96.49122807"]};
let result = Object.keys(data)
.reduce((a, c, i) => (a[i] = [c, ...data[c]], a), {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
为什么需要一个带有索引的对象(如数组)?数组解决方案:
let list = {'Achievement': ["110", "100", "104", "110"],'Emp Code' : ["1000001", "1000001", "1000001", "1000001"],'Product' :["Product A ", "Product B", "Product A ", "Product B"],'Reportee Name' :["Harry", "Harry", "Peter", "Peter"],'Target' : ["116", "94", "105", "114"],'percentage': ["94.82758621", "106.3829787", "99.04761905", "96.49122807"]};
let result = []
for (let key in list) {
result.push([key, ...list[key]])
}
console.log(result)