1

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?

shampoo
  • 1,173
  • 12
  • 22
  • 1
    Possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Luca Kiebel May 04 '18 at 15:59
  • You changed the nature of the question quite a bit with your edit. –  May 04 '18 at 16:50

4 Answers4

1

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";
zfrisch
  • 8,474
  • 1
  • 22
  • 34
1

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.

  • Isn't there a way to do the assignment by reference? – shampoo May 04 '18 at 16:04
  • No, all assignment is "by value". Closest you could get would be to nest each `people` object in another object, and assign that reference type to a variable. But that doesn't seem like a good solution. What's the actual problem at hand? –  May 04 '18 at 16:05
1

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;}
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
  • Thanks, `Object.assign(newjack,jack)` was what I wanted. – shampoo May 04 '18 at 16:07
  • BTW, the `find` is not necessary, neither is the `let` and `const` even a `var` would be fine. `Object.assign` would do the trick. – shampoo May 04 '18 at 16:20
  • As you are using `people[0]` so you can easily do using Object.assign(). See updated answer. – Narendra Jadhav May 04 '18 at 16:24
  • @shampoo: Be aware that this won't handle cases where you want properties removed. This isn't a magic method that gives you the functionality you were looking for. It's just a convenience for assigning properties of one object to another. –  May 04 '18 at 16:51
0

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.

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103