0

I want to replace a variable in an object array with another name,

let arr={name:'ash', id:2}

I want to replace it as arr = {newname:'ash', id:2}

Lex V
  • 1,414
  • 4
  • 17
  • 35

2 Answers2

0

There are two options:

1.

let name = arr.name
arr = {...arr, name: null, newname:name}

You can use omit function from lodash library, and this option is better. Here is docimentation of omit function

Svaba1012
  • 26
  • 7
0

let arr={name:'ash', id:2}

let newname = arr.name;
delete arr.name

arr['newname'] = newname

console.log(arr)

You can use a variable to store the value of name ,then use Object operator delete the key of name

Cognia
  • 437
  • 3
  • 10