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?
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?
- 1 Possible duplicate of Is JavaScript a pass-by-reference or pass-by-value language? – Luca Kiebel Commented May 4, 2018 at 15:59
- You changed the nature of the question quite a bit with your edit. – user2437417 Commented May 4, 2018 at 16:50
4 Answers
Reset to default 1As 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;}
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";
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.
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745453568a4628382.html
评论列表(0条)