js修改对象数组中的对象属性名
将数组中所有id变为code,所有title变为name
<script>
const list = [{
id: 'a',
title: 'A'
}, {
id: 'b',
title: 'B',
children: [{
id: 'c',
title: 'C'
}, {
id: 'd',
title: 'D'
}]
}]
</script>
通过数组方法map()修改属性名。
let result2 = [];
let s = list.map(item => {
let res = {};
res.code = item.id;
res.name = item.title;
if (item.children) {
res.children = item.children.map(data => { return { code: data.id, name: data.title } });
}
return res;
})
console.log(s);