1

I want to swap the key/value content of Jacob with the key/value content of Guillermo

I was able to swap Guillermo to Jacob but not the other way around.

This is my code which is only half correct:

let students = {
  jacob: {
    classes: ["math", "chemistry", "english"],
    grade: 11,
    age: 16,
  },
  guillermo: {
    classes: ["history", "math", "physics"],
    grade: 12,
    age: 17,
  },
};
let temp = students.jacob;
students.guillermo = temp;
let temp1 = students.guillermo;
students.jacob = temp1;

console.log(students)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
misseal
  • 11
  • 1
  • You should first get both the `temp` variables assigned, and only then make assignments to the object properties. Just look at the middle two assignments as you have them now, and let that sink in... – trincot Jun 15 '20 at 14:05
  • Does this answer your question? [How do I correctly clone a JavaScript object?](https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object) – Heretic Monkey Jun 15 '20 at 14:06

1 Answers1

0

You need to copy before re-assigning

let students = {
  jacob: {
    classes: ["math", "chemistry", "english"],
    grade: 11,
    age: 16,
  },
  guillermo: {
    classes: ["history", "math", "physics"],
    grade: 12,
    age: 17,
  },
};
let temp = students.jacob;
let temp1 = students.guillermo;
students.guillermo = temp;
students.jacob = temp1;
console.log(students)
mplungjan
  • 169,008
  • 28
  • 173
  • 236