Spread Assignment Operator

I'm proposing a spread-like operator ...= for (re)assignment on Arrays or Objects

Similar to the existing convention of regenerative consolidation:

/* Arrays */
let x = [1, 2, 3];
x = [...x, 4, 5, 6];
// Result: [1, 2, 3, 4, 5, 6]
/* Objects */
let y = {a:1, b:2, c:3};
y = {...y, {b:5, c:6, d:7}}
// Result: {a:4, b:5, c:6, d:7}

I propose the following shorthand syntax for spread (re)assignment

/* Arrays will act in a manner of bulk appending values */
let x = [1, 2, 3];
x ...= [4, 5, 6];
// Logic: [1, 2, 3, ...[4, 5, 6]];
// Result: [1, 2, 3, 4, 5, 6]
/* Objects will act in a manner of bulk `property:value` (re)assignment */
let y = {a:1, b:2, c:3};
y ...= {b:5, c:6, d:7};
// Logic: {a:1, b:2, c:3, ...{b:5, c:6, d:7}};
// Result: {a:4, b:5, c:6, d:7}

With this proposed syntax, I suggest the ability to (re)populate Arrays and Objects (using this operator) without having to fully re-assign the variable, thus allowing for usage on arrays and objects declared using const

The following syntax will only work on variables declared using var and let. It will throw an error for variables declared using const

1 Like

An assignment will never work on a const variable. If you really suggest that ...= should modify the lefthandside object/array, this functionality already exists:

const x = [1, 2, 3];
x.push(4, 5, 6); // or `x.push(...array)`

const y = {a:1, b:2, c:3};
Object.assign(y, {b:5, c:6, d:7});

I modified my examples to focus on the main intentions of the proposal and drop the request for const compatibility