const dataset = {
"diamonds":77,
"gold-bars":28,
"exciting-stuff":52,
"oil":51,
"sports-cars":7,
"bitcoins":40
};
// 想要输出一下格式
const output = [
{"asset":"diamonds", "quantity":77},
{"asset":"gold-bars", "quantity":28},
{"asset":"exciting-stuff", "quantity":52},
{"asset":"oil", "quantity":51},
{"asset":"bitcoins", "quantity":40}
];
const dataset = {
"diamonds":77,
"gold-bars":28,
"exciting-stuff":52,
"oil":51,
"sports-cars":7,
"bitcoins":40
};
// 方法:
const splitKeyValue = obj => {
const keys = Object.keys(obj);
const res = [];
for(let i = 0; i < keys.length; i++){
res.push({
'asset': keys[i],
'quantity': obj[keys[i]]
});
};
return res;
};
console.log(splitKeyValue(dataset));
javascript 将键和值拆分为单独的对象
最新推荐文章于 2023-12-27 21:00:00 发布