如何使用`map()`方法从数组中的对象中移除对象属性。

huangapple go评论63阅读模式
英文:

How to remove object properties from objects in array using map()

问题

let objArr = [ { "name" : "Rohan", "request": true}, { "name" : "Sohan", "request": true, "modify" : "today", "expire": "tomorrow"}, { "name" : "Mohan", "request": true, "modify" : "today", "expire": "tomorrow"} ];

const newArr = objArr.map(v => ({ ...v, oldName: v.name, newName: v.name + '_copy', newRecord: true, request: v.request }))

console.log(newArr)
英文:
let objArr = [ 	{ "name" : "Rohan", "request": true}, 	{ "name" : "Sohan", "request": true, "modify" : "today", "expire": "tomorrow"}, 	{ "name" : "Mohan", "request": true, "modify" : "today", "expire": "tomorrow"} ];

const newArr = objArr.map(v => ({ ...v, oldName: v.name, newName: v.name + '_copy', newRecord: true }))

console.log(newArr)

How to remove expire, modify, from the array?

Only oldName, newName, newRecord and request should display, the rest we can disable/remove.

答案1

得分: 1

不要删除某些属性,可以仅复制所需的属性,如下所示:

const newArr = objArr.map(v => ({ oldName: v.name, newName: v.name, newRecord: true }));
英文:

Rather than deleting some attributes, you can copy only those properties which are required as shown below

const newArr = objArr.map(v => {oldName: v.name, newName: v.name, newRecord: true})

huangapple
  • 本文由 发表于 2023年2月18日 23:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定