-
转换方法
/** * arr:数组 * key:新key名 * replaceKey:原始key名 **/ function replaceObjectKey(arr, key, replaceKey) { let newArr = []; arr.forEach((item, index) => { for (var i = 0; i < key.length; i++) { item[key] = item[replaceKey]; } // 删除原始key名 delete item[replaceKey]; newArr.push(item); }); return newArr; }
-
使用方法:
let arr = [ { id: 1, address: "南京", }, { id: 2, address: "合肥", }, { id: 3, address: "上海", }, ]; let newArr = replaceObjectKey(arr, "label", "address"); console.log(newArr);
-
打印结果
JS - 修改数组中对象的键名
于 2021-12-08 17:31:12 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)