js 改变对象的key
//oldMap要改变的对象,newKey改变的key
let newMap = this.replaceMapKey({quantity:'20',amout:'200'},newQuantity,newAmount)
replaceMapKey(oldMap, newKey, newKey1) {
var keyMap = {quantity: newKey, amount: newKey1};
var objs = Object.keys(oldMap).reduce((newData, key) => {
let newKey = keyMap[key] || key
newData[newKey] = oldMap[key]
return newData
}, {});
return objs;
},
console.log(newMap)
输出结果为:
{newQuantity:'20',newAmount:'200'}