目录
- 一、重命名对象的key
- 1、renameKeys
- 2、replace方法替换指定名称
- 3、replace进阶版替换
- 4、封装方法renameObjectKey遍历替换
- 5、利用迭代方法reduce替换key
- 6、遍历最小差异化替换key,删除原属性
- 7、对数组替换key的操作(map递归)
一、重命名对象的key
1、renameKeys
console.log('-----------------方法1-----------------');
const obj = {a: "小明", b: "20"};
const newKeys = {a: "name", b: "age"};
console.log('要转化的数据:\n',obj);
const renamedObj = renameKeys(obj, newKeys);
console.log('结果:\n',renamedObj);
console.log('--------------------------------------');
- 结果
{a: “小明”, b: “20”} 改成 {name: ‘小明’, age: ‘20’}
2、replace方法替换指定名称
console.log('-----------------方法2---------------');
let object = {name: "小明", id: 20, sex: false};
console.log('要转化的数据:\n',object);
let str = JSON.stringify(object);
str = str.replace('name', 'mingzi');
str = str.replace('id', 'age');
object = JSON.parse(str);
console.log('结果:\n',object);
console.log('--------------------------------------');
- 结果
name替换成了mingzi ,id替换成了age
3、replace进阶版替换
console.log('-----------------方法2进阶---------------');
let object1 = {name: "小明", id: 20, sex: false};
const targetKeys = ['MingZi','Age','Sex'];
const keys = [];
for (var key in object1) {
keys.push(key);
}
console.log('要转化的数据:\n',object1);
let str1 = JSON.stringify(object1);
for (let i = 0;i<targetKeys.length;i++){
str1 = str1.replace(keys[i],targetKeys[i])
}
object1 = JSON.parse(str1);
console.log('结果:\n',object1);
console.log('----------------------------------------');
- 结果
name替换成了MingZi
id替换成了Age
sex替换成了XingBie
4、封装方法renameObjectKey遍历替换
console.log('-----------------方法4---------------');
function renameObjectKey(oldObj, oldName, newName) {
const newObj = {};
Object.keys(oldObj).forEach(key => {
const value = oldObj[key];
if (key === oldName) {
newObj[newName] = value;
} else {
newObj[key] = value;
}
});
return newObj;
}
let object2 = {name: "小明", id: 20, sex: false};
console.log('要转化的数据:\n',object2)
object2 = renameObjectKey(object2, 'id', 'age');
console.log('结果:\n',object2);
console.log('--------------------------------------');
- 结果
id替换成了age
5、利用迭代方法reduce替换key
console.log('-----------------方法5---------------');
let object3 = {key1: "小明", key2: 20, key3: false};
const keyMap = {key1: "name", key2: "age", key3: "sex"};
console.log('要转化的数据:\n',object3)
const mappedData = Object.keys(keyMap).reduce((obj, k) => Object.assign(obj, {[keyMap[k]]: object3[k]}), {});
console.log('结果:\n',mappedData);
console.log('--------------------------------------');
- 结果
key1、key2、key3替换成了 name、age、sex
6、遍历最小差异化替换key,删除原属性
console.log('-----------------方法6---------------');
let object4 = {
"_id": "00607",
"_email": "user1@gmail.com",
"name": "小明"
};
console.log('要转化的数据:\n',object4)
const new_key = "id";
const old_key = "_id";
Object.keys(object4).forEach(key => {
if (key === old_key) {
object4[new_key] = object4[key];
delete object4[key];
} else {
object4[`_${key}`] = object4[key];
delete object4[key];
object4[`${key}`] = object4[`_${key}`];
delete object4[`_${key}`];
}
});
console.log('结果:\n',object4);
console.log('--------------------------------------');
- 结果
_id替换成了id
7、对数组替换key的操作(map递归)
console.log('-----------------方法7-----------------');
let a = ["code", "name", "date"];
let b = [{m1: 2, m2: 3, m3: 4, m4: 8}, {c1: 2, c2: 3, c3: 4}, {a1: 'knn', a2: 3, a3: 4, a4: 19}];
console.log('要转化的数据:\n', b)
let c = b.map((item, index, arr) => {
let obj = {};
var mm1 = 0;
for (let i in item) {
if (mm1 < a.length){
obj[a[mm1]] = item[i];
mm1++;
}else {
obj[i] = item[i];
}
}
return obj;
});
console.log('结果:\n', c);
console.log('--------------------------------------');
- 结果
将数组b中的key替换成指定a = [“code”, “name”, “date”]的key