0

I have an object like,

let axis = {
    x: -1,
    y: -1
}

and I want to create another object like,

let node = {
    //axis object's variable(1) 
    //axis object's variable(2)
    //axis object's variable(3)
}

How I can create object like above in javascript?

  • 1
    You could use object spread: `let node = {...axis}` for a shallow copy. However, *"How to copy an object in JavaScript"* has been asked **plenty** of times. In the future before asking a question, please do some research. Possible duplicate of [How do I correctly clone a JavaScript object?](https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object) – Tyler Roper Jan 08 '19 at 04:24
  • 1
    Possible duplicate of [How do I correctly clone a JavaScript object?](https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object) – Just code Jan 08 '19 at 04:27

2 Answers2

0

Is this what you are after?

let axis = {
  x: -1,
  y: -1
}

let node = {
  x: axis.x,
  y: axis.y
}

console.log(node.x); // prints -1 to the console
console.log(node.y); // prints -1 to the console
100pic
  • 387
  • 3
  • 13
-1

You can use spread to do such task.

let axis = {
    x: -1,
    y: -1
}

let node = {
    ...axis
}

console.log(node)
holydragon
  • 6,158
  • 6
  • 39
  • 62