shampoo
shampoo

Reputation: 1281

How to reassign an object so that it changes the referenced object as well?

Let's say I have an array of objects:

var people = [
  {name: "Jack", height: 180},
  {name: "Marry", height: 170},
  {name: "Susan", height: 162},
]

If I take the first person and change the height like this:

var jack = people[0];     
jack.height = 163;

Then this change is reflected in the object in the array as well like this:

people = [
      {name: "Jack", height: 163},
      {name: "Marry", height: 170},
      {name: "Susan", height: 162},
    ] 

However if I reassign the object like this

 jack = {name: "Jack the developer", height: 163}

The array doesn't change:

people = [
      {name: "Jack", height: 163},
      {name: "Marry", height: 170},
      {name: "Susan", height: 162},
    ] 

How should I assign jack so that it changes the reference?

Upvotes: 2

Views: 6506

Answers (4)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

In JavaScript objects are passed and assigned by reference so

var jack = people[0];

here jack and people are both references to the same object.

So in first case,

When you modify the value like this jack.height = 163 then people object also get the reflected value.

in your second case,

With this line jack = {name: "Jack the developer", height: 163}; you're creating totally a new object jack with new value which will not reflect the people object that's why here you're not getting the re-assigned value.

Upvotes: 0

Narendra Jadhav
Narendra Jadhav

Reputation: 10262

As per your way you can do using Object.assign()

DEMO

const arr=[
      {name: "Jack", height: 163},
      {name: "Marry", height: 170},
      {name: "Susan", height: 162},
    ] ,
    jack = {name: "Jack the developer", height: 163};
    
Object.assign(arr[0],jack);

console.log(arr);
.as-console-wrapper {max-height: 100% !important;top: 0;}


You can also use find method of array and merge new value using Object.assign().

DEMO

const arr=[
      {name: "Jack", height: 163},
      {name: "Marry", height: 170},
      {name: "Susan", height: 162},
    ] ,
    jack = {name: "Jack the developer", height: 163};
    
let result = arr.find(({name})=>name=="Jack");

if(result){
Object.assign(result,jack);
};

console.log(arr);
.as-console-wrapper {max-height: 100% !important;top: 0;}

Upvotes: 1

user2437417
user2437417

Reputation:

JS does have reference types, which is why this code works:

var jack = people[0];    
jack.height = 163;

However, it still does assignments "by value" instead of "by reference", which means that jack holds the value of the reference type (the object) but if you reassign to jack, you're just reassigning a new reference value to a new object.

If JS had assignment "by reference", then your code would work, because jack would be referencing the original location of the object in the array, allowing you to work with jack as though you were working with that array index. That's not the case in JS.

Upvotes: 1

zfrisch
zfrisch

Reputation: 8660

When you do this:

jack = {name: "Jack the developer", height: 163};

You're creating a new object and assigning it to jack, instead of changing the current object, E.g.:

jack.name = "John";

Upvotes: 1

Related Questions