当前位置:网站首页>js修改对象数组的key值
js修改对象数组的key值
2022-07-27 03:22:00 【Lemon今天学习了吗】
实例:将dataOld这个对象数组的对象中的属性key变成dataNew中的形式;
//原数据
dataOld: [{ count: '33', area: '122.2', districtId: 43000 }, { count: '44', area: '168.2', districtId: 43001 }]
//接口需要的数据
dataNew: [{ countAll: '33', countArea: '122.2', districtId: 43000 }, { count: '44', area: '168.2', districtId: 43001 }]方法一:使用map循环,在map循环内创建一个新对象,将item要改变的key赋给新创建的对象里面新key值,然后push给一个新创建的数组dataNew即可;
let dataNew = []; //新数组
dataOld.map(item => {
let obj = {
countAll: item.count,
countArea: item.area,
districtId: item.districtId,
}
dataNew.push(obj);
});方法二:使用map循环 + replace替换,通过循环然后将子类JSON.stringify后采用replace来改变属性key;
let dataNew = []; //新数组
dataOld.map(item => {
let _item = JSON.parse(JSON.stringify(item).replace('count', 'countAll').replace('area', 'countArea'));
dataNew.push(_item)
});
方法三:使用forEach循环 + for循环,通过Object.keys()来改变属性key;
convertKey (arr, key) {
let dataNew = []; //新数组
this.dataOld.forEach((item, index) => {
let obj = {}; //新数组里的新对象
for (var i = 0; i < key.length; i++) {
obj[key[i]] = item[Object.keys(item)[i]]; //key值替换
}
dataNew.push(obj);
})
console.log(dataNew,'dataNew');
return dataNew;
},
// 改变后的key
let dataNew = this.convertKey(this.dataOld, ['countAll', 'countArea', 'districtId']);结果:

边栏推荐
- 细说Hash(哈希)
- Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
- 【OBS】circlebuf
- Detailed analysis of trajectory generation tool in psins toolbox
- leetcode:433. 最小基因变化
- js实现页面跳转与参数获取加载
- Restful fast request 2022.2.2 release, supporting batch export of documents
- 整理字符串
- Subject 3: Jinan Zhangqiu line 5
- 利用LCD1602显示超声波测距
猜你喜欢

An online duplicate of a hidden bug

Big talk · book sharing | lean product development: principles, methods and Implementation

技术分享 | 需要小心配置的 gtid_mode

JMeter download and installation

Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!

【小样本分割】MSANet: Multi-Similarity and Attention Guidance for Boosting Few-Shot Segmentation

《MySQL》认识MySQL与计算机基础知识

Lixia action | Yuanqi Digitalization: existing mode or open source innovation?

Parallels Desktop启动虚拟机“操作失败”问题解决

STM32CubeMX学习笔记(41)——ETH接口+LwIP协议栈使用(DHCP)
随机推荐
Leetcode daily question: relative sorting of arrays
通信协议综述
整理字符串
Principle of bean validation --07
The real digital retail should have richer connotation and significance
Interview question 02.05. sum of linked list
【MySQL系列】MySQL索引事务
C language learning notes - memory management
Subject 3: Jinan Zhangqiu line 2
11.zuul路由网关
JS to realize page Jump and parameter acquisition and loading
356页14万字高端商业办公综合楼弱电智能化系统2022版
Slope of binary tree
Elastic认证考试:30天必过速通学习指南
二叉树的坡度
Subject 3: Jinan Zhangqiu line 3
Detailed analysis of trajectory generation tool in psins toolbox
"Gonna be right" digital collection is now on sale! Feel the spiritual resonance of artists
Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!
【OBS】circlebuf