skip to Main Content

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}

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search