我有一个这样的对象:
{
"ABC":{"minValue":0,"maxValue":5},
"XYZ":{"minValue":0,"maxValue":5},
"PQR":{"minValue":0,"maxValue":5},
"overall":{"minValue":0,"maxValue":5}
}
我正在尝试制作一个像这样的简单数组
[
["type":"ABC","minValue":0,"maxValue":5],
["type":"XYZ","minValue":0,"maxValue":5],
["type":"PQR","minValue":0,"maxValue":5],
["type":"overall","minValue":0,"maxValue":5]
]
不知道如何获得它。
这是我的尝试:
var s = scoreFilters; //my json object
var out = Object.keys(s).map(function(data){
console.log(data)
return [["type":data,"minValue":data.minValue,"maxValue":data.maxValue]];
});
答案 0 :(得分:2)
您可以像这样使用Object.entries
,map
和spread syntax:
let input = {"ABC":{"minValue":0,"maxValue":5},"XYZ":{"minValue":0,"maxValue":5},"PQR":{"minValue":0,"maxValue":5},"overall":{"minValue":0,"maxValue":5}}
let output = Object.entries(input).map(([type, values]) => ({ type, ...values}))
console.log(output)
答案 1 :(得分:2)
假设您想要输出一个对象数组,则可以.map
Object.entries
输入:
const obj = {
"ABC":{"minValue":0,"maxValue":5},
"XYZ":{"minValue":0,"maxValue":5},
"PQR":{"minValue":0,"maxValue":5},
"overall":{"minValue":0,"maxValue":5}
};
const arr = Object.entries(obj).map(([type, { minValue, maxValue }]) => ({
type,
minValue,
maxValue
}));
console.log(arr);
答案 2 :(得分:1)
您可以使用Object.entries()
和.map()
来获取对象数组:
const data = {
"ABC": {"minValue": 0,"maxValue": 5},
"XYZ": {"minValue": 0,"maxValue": 5},
"PQR": {"minValue": 0,"maxValue": 5},
"overall": {"minValue": 0,"maxValue": 5}
};
const result = Object.entries(data)
.map(([ k, v ]) => Object.assign({}, {type: k}, v));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }