js对象替换键值名称
将obj中的id和name字段替换分别替换成为“@id”,“@name”
代码如下:
let obj = [{id:1,name:"张三"},{id:2,name:"李四"}].map(function (item) { return{ "@id":item.id, "@name":item.name } })
对于对象
let obj = { id:1, name:"张三", age:20, grade:{ Math :90, English :80 } }
例如:替换obj中grade属性,将Math替换Chinese,将English替换成Chemistry
完整代码为:
let obj = { id:1, name:"张三", age:20, grade:{ Math :90, English :80 } } obj.grade["Chinese"] = obj.grade.Math; obj.grade["Chemistry"] = obj.grade.English; delete obj.grade.Math; delete obj.grade.English; console.log(obj); //{ id: 1,name: '张三',age: 20,grade: { Chinese: 90, Chemistry: 80 } }