var color3 = {
name: 'Black',
type: 'special',
rating: 1
};
var rateColor3 = (colorName, Nrating) => ({ ...colorName, Nrating });
console.log(color3.rating); ///// 1
console.log(rateColor3(color3, 6).rating);//////1
console.log(color3.rating);//////1
I want to use object spread operator while keeping color3 immutable, But the output of my 2nd console.log is 1 instead of 6, what am i doing wrong?
You are assigning a the value 6
to the key NRating
to the object, not the existing rating
.
So your object would look like this:
{
name: 'Black',
type: 'special',
rating: 1,
Nrating: 6
}
To override the existing rating
property, you have to do:
var rateColor3 = (colorName, Nrating) => ({ ...colorName, rating: Nrating });
or change your parameter Nrating
to rating
.
var color3 = {
name: 'Black',
type: 'special',
rating: 1
};
var rateColor3 = (colorName, rating) => ({ ...colorName, rating });
console.log(rateColor3(color3, 6));