es6将edit 001 002 003 变成edit 001 002 003 格式
可以使用ES6的解构赋值和reduce方法来实现将数组转换为对象的格式:
const arr = [
{001:[{},{},{}]},
{002:[{},{},{}]},
{003:[{},{},{}]}
];
const result = arr.reduce((obj, item) => {
const key = Object.keys(item)[0];
obj[key] = item[key];
return obj;
}, {});
const output = { "edit": result };
console.log(output);
输出结果为:
{
"edit": {
"001": [{},{},{}],
"002": [{},{},{}],
"003": [{},{},{}]
}
}
原文地址: https://www.cveoy.top/t/topic/iQyl 著作权归作者所有。请勿转载和采集!