当前位置:网站首页>JS modify the key value of the object array
JS modify the key value of the object array
2022-07-27 04:24:00 【Did lemon study today】
example : take dataOld Properties in the object of this object array key become dataNew In the form of ;
// The original data
dataOld: [{ count: '33', area: '122.2', districtId: 43000 }, { count: '44', area: '168.2', districtId: 43001 }]
// Data required by the interface
dataNew: [{ countAll: '33', countArea: '122.2', districtId: 43000 }, { count: '44', area: '168.2', districtId: 43001 }]Method 1 : Use map loop , stay map Create a new object in the loop , take item To change key Assign new to the newly created object key value , then push Give a newly created array dataNew that will do ;
let dataNew = []; // New array
dataOld.map(item => {
let obj = {
countAll: item.count,
countArea: item.area,
districtId: item.districtId,
}
dataNew.push(obj);
});Method 2 : Use map loop + replace Replace , Pass the loop and then subclass JSON.stringify After using replace To change properties key;
let dataNew = []; // New array
dataOld.map(item => {
let _item = JSON.parse(JSON.stringify(item).replace('count', 'countAll').replace('area', 'countArea'));
dataNew.push(_item)
});
Method 3 : Use forEach loop + for loop , adopt Object.keys() To change properties key;
convertKey (arr, key) {
let dataNew = []; // New array
this.dataOld.forEach((item, index) => {
let obj = {}; // New object in new array
for (var i = 0; i < key.length; i++) {
obj[key[i]] = item[Object.keys(item)[i]]; //key Value substitution
}
dataNew.push(obj);
})
console.log(dataNew,'dataNew');
return dataNew;
},
// After the change key
let dataNew = this.convertKey(this.dataOld, ['countAll', 'countArea', 'districtId']);result :

边栏推荐
猜你喜欢

How CentOS installs mysqldump

深度剖析 —— 动态内存管理

使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)

Echart柱状图中数据显示在图上方
![[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)](/img/6a/02c76f5bd35b2d89e4f471b9b688f5.png)
[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)

Learning route from junior programmer to architect + complete version of supporting learning resources

F - Pre-order and In-order(Atcoder 255)

Rust:axum learning notes (1) Hello World
![[small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation](/img/b9/270e0f20586a953e83a18f7fac155f.png)
[small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation
随机推荐
Px4 module design 12: high resolution timer design
2022 operation of simulated examination question bank and simulated examination platform for safety production management personnel of hazardous chemical production units
Elastic开源社区:开发者招募
人很话不多,工程师不耍嘴皮子
[Code] sword finger offer 04 search in two-dimensional array
tcp协议知识详解
数据分析师岗位分析
MySQL: understand the basic knowledge of MySQL and computer
Rust:axum学习笔记(1) hello world
[leetcode] day104 no overlapping interval
Is VR panoramic production a single weapon in the home decoration industry? Why is this?
centos如何安装mysqldump
搜索旋转排序数组
[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)
新互联网时代已来 WEB 3.0 会给我们带来哪些新机遇
E-commerce system combined with commodity spike activities, VR panorama continues to bring benefits
项目参数做成可配置项,@ConfigurationProperties注解的使用
xxx is not in the sudoers file. This incident will be reported
每日一题:从链表中删去总和值为零的连续节点
ASP voice notification interface docking demo