The properties are added in order, so if you want to override existing properties, you need to put them at the end instead of at the beginning:

return {
  value: {
    ...initialState,
    ...newObject
  }
}

You don't need newObject (unless you already have it lying around), though:

return {
  value: {
    ...initialState,
    isAvailable: newValue
  }
}

Example:

const o1 = {a: "original a", b: "original b"};
// Doesn't work:
const o2 = {a: "updated a", ...o1};
console.log(o2);
// Works:
const o3 = {...o1, a: "updated a"};
console.log(o3);
Answer from T.J. Crowder on Stack Overflow
🌐
Dillion's Blog
dillionmegida.com › p › spread-operator-simplified
Spread Operator in JavaScript, Simplified - Dillion's Blog
console.log(fullInfo) // { // name: 'Micheal', // age: 100, // language: 'JavaScript', // stack: 'frontend' // } In this example, the name property of fullInfo comes after the spread info object, so the name of fulInfo would overwrite the name coming from the info object. The spread operator can also be used to spread arrays as function arguments.
🌐
Reddit
reddit.com › r/learnjavascript › how to avoid overwriting nested object with spread operator?
r/learnjavascript on Reddit: How to avoid overwriting nested object with spread operator?
July 2, 2020 -

b is overwritten:

const shared = { a : { b : 1 } }
const added = { ...shared, a : { c : 2 } }

console.log(added);

Actual use case is below. I was testing the above in JSBin because I thought this might be a concern.

How can I avoid overwriting b (or voice in my real example)? I want to set the ssmlGender in shared options, but not the languageCode.

I'm thinking I might have to set properties one by one using dot or bracket notation? Or is there a more concise way?

      const sharedOptions = {
         , voice : { languageCode : "", ssmlGender : voiceGender.male }
         , audioConfig : {audioEncoding : "OGG_OPUS"}
      }

      const foreignAudioOptions = {
         ...sharedOptions
         , input : {text : ""}
      }

      const englishAudioOptions = {
         ...sharedOptions
         , input : {text : ""}
      }
🌐
DEV Community
dev.to › dailydevtips1 › javascript-overwrite-property-in-an-object-3g32
JavaScript clone and rewrite property from existing object - DEV Community
July 7, 2022 - If we, for instance, put the property we want to overwrite. First, it won't work. This is because the latter assignments always win. console.log({ online: true, ...user }); // { online: false, username: 'Chris' } And that's the easiest way to overwrite an object value using JavaScript's spread operator.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Spread_syntax
Spread syntax (...) - JavaScript | MDN
All primitives can be spread in objects. Only strings have enumerable own properties, and spreading anything else doesn't create properties on the new object. ... When using spread syntax for function calls, be aware of the possibility of exceeding the JavaScript engine's argument length limit.
🌐
Clhenrick
clhenrick.io › blog › es6-spread-operator-mishap
Avoiding Mishaps with the ES6 Spread Operator – Chris Henrick
and we’d end up with the desired output. This is because Object.assign() merges properties of objects from left to right, so whatever object to the right will overwrite whatever object is to the left when the two objects have a shared property.
🌐
Appsmith
community.appsmith.com › content › guide › javascript-spread-operator-what-are-those-three-dots-my-code
The Javascript Spread Operator - What Are Those Three Dots in My Code? | Appsmith Community Portal
August 29, 2023 - When spreading properties from one object into another, if there are properties with the same name, the last property encountered in the spread operation will overwrite the earlier ones.
🌐
Execute Program
executeprogram.com › courses › everyday-typescript › lessons › object-spread
Everyday TypeScript: Object Spread
Learn programming languages like TypeScript, Python, JavaScript, SQL, and regular expressions. Interactive with real code examples.
🌐
The Code Barbarian
thecodebarbarian.com › object-assign-vs-object-spread.html
Object.assign vs Object Spread in Node.js
You can also mix in other properties with the object spread operator. Order matters: the object spread operator will overwrite properties that are defined before it, but not after.
Find elsewhere
🌐
Rock Your Code
rockyourcode.com › use-the-spread-operator-to-update-objects
Use the Spread Operator to Update Objects | rockyourcode
September 30, 2021 - With spreading, you can change a property non-destructively: You make a copy of the object where the property has a different value. For example, this code non-destructively updates property .foo: const obj = { foo: "a", bar: "b" }; const updatedObj ...
🌐
Dmitri Pavlutin
dmitripavlutin.com › object-rest-spread-properties-javascript
An Easy Guide to Object Rest/Spread Properties in JavaScript
January 3, 2018 - Now part1 and part3 have a new property .configuration: ... The first object spread ...part1 sets the value of .configuration to 'sedan'. Nevertheless the latter object spread ...part3 overwrites the previous .configuration value, making it finally 'hatchback'.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Spread Operator(Object inside Object) - JavaScript - The freeCodeCamp Forum
October 8, 2019 - I’m trying to use the spread operator to change only the city property of the address Object If I try const user2 = {...user, address: { city: "Quebec"}}; I’m gonna lose the other properties of address Object. Any hint to solve problems with Objects inside Objects · Wouldn’t that modify ...
🌐
DEV Community
dev.to › mlgvla › javascript-using-the-spread-operator-with-nested-objects-2e7l
JavaScript: Using the spread operator with nested objects - DEV Community
February 14, 2022 - Objects in Javascript are passed by reference, not value. The top-level object and each nested object of newObject share the exact same locations in memory to those of object. Passing by reference means you are assigning the address location to newObject. Make a change in newObject, and you change object. Fortunately, the spread operator ...
🌐
Daily Dev Tips
daily-dev-tips.com › posts › javascript-overwrite-property-in-an-object
JavaScript clone and rewrite property from existing object
July 6, 2022 - If we, for instance, put the property we want to overwrite. First, it won’t work. This is because the latter assignments always win. console.log({ online: true, ...user }); // { online: false, username: 'Chris' } And that’s the easiest way to overwrite an object value using JavaScript’s spread operator.
🌐
Lucybain
lucybain.com › blog › 2018 › js-es6-spread-operator
Lucy | JS: ES6’s spread operator for objects
Just like before, the { starts a new object. Then using the spread operator on cat adds all the cat properties to the new object. Our new sound: "woof" overwrites the existing sound property from cat.
🌐
YouTube
youtube.com › watch
ES6 Spread Operator: Simplify Array and Object Manipulation in JavaScript - YouTube
The ES6 Spread Operator (those three magic dots ...) is here to save the day! This video will be your one-stop guide to mastering the spread operator and w...
Published   April 17, 2024
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-easily-update-your-objects-with-the-spread-operator-8f3dfdbcf956
How to Easily Update Your Objects with the Spread Operator | by Piero Borrelli | JavaScript in Plain English
July 30, 2024 - How to Easily Update Your Objects with the Spread Operator Shaping, modifying and updating them as you wish The spread operator has been a little earthquake in the JavaScript world. It allows you to …