将数组对象中的英文属性名替换为中文属性名
const arr = [
{name: '张三', age: 18, address: '湖北'},
{name: '李四', age: 18, address: '安徽'},
{name: '王五', age: 18, address: '北京'}
]
const map = { name: '姓名', age: '年龄', address: '地址'}
function change(arr, map) {
// 在这里写代码
}
const arr2 = change(arr, map)
console.log(arr2)
// [ 结果效果:
// {'姓名': '张三', '年龄': 18, '地址': '湖北'},
// {'姓名': '李四', '年龄': 18, '地址': '安徽'},
// {'姓名': '王五', '年龄': 18, '地址': '北京'}
// ]
解决:
<script>
const arr = [
{ name: '张三', age: 18, address: '湖北' },
{ name: '李四', age: 18, address: '安徽' },
{ name: '王五', age: 18, address: '北京' }
]
const map = { name: '姓名', age: '年龄', address: '地址' }
function change (arr, map) {
// 在这里写代码
let newArr = []
arr.map(arrItem => {
let obj = {}
for (let k in map) {
obj[map[k]] = arrItem[k]
}
newArr.push(obj)
})
return newArr
};
const arr2 = change(arr, map)
console.log(arr2)
</script>
结果: