Multi-index assignment.

# /#!/JoePea (7 years ago)

I always run into situations where I'd like to assign some things from another array.

For example,

array[0] = 1
array[1] = 2

could be written as

array[0, 1] = [1, 2]

and maybe perhaps also specific access on the right hand side too:

array[1] = otherArray[3]
array[4] = otherArray[5]
// can be
array[
​1​
,
​4​
] = [
​
otherArray[3]
​
,
​
otherArray[
​5​
]
​
]
​// but also a shorter form:

array[1, 4] = otherArray[3, 5]
# Jordan Harband (7 years ago)

Can you elaborate on these situations?

In general, I find that using specific array indexes is most often a code smell.

# T.J. Crowder (7 years ago)

On Mon, Dec 4, 2017 at 7:21 PM, /#!/JoePea <joe at trusktr.io> wrote:

I always run into situations where I'd like to assign some things from another array.

...

could be written as

array[0, 1] = [1, 2]

No, it couldn't, because that's already valid syntax for setting array[1] to the new array [1, 2]. (Comma operator turns 0, 1 into 1.)

You can use destructuring, but it's longer than the individual assignments would be:

[array[0], array[1]] = [1, 2];

-- T.J. Crowder

# Alex Vincent (7 years ago)

---------- Forwarded message ---------- From: Jordan Harband <ljharb at gmail.com> To: "/#!/JoePea" <joe at trusktr.io> Cc: es-discuss <es-discuss at mozilla.org> Bcc: Date: Mon, 4 Dec 2017 11:28:22 -0800 Subject: Re: Multi-index assignment. Can you elaborate on these situations?

In general, I find that using specific array indexes is most often a code smell.

Agreed, and depending on the programming language, commas in square brackets has a different meaning. Way back when I was a child working with BASIC (I think), foo[1, 2] meant a two-dimensional array, what we now call foo[1][2].

It sounds like you are trying to abuse object destructuring here... in a way that is probably dangerous.

# medikoo (7 years ago)

Just do:

Object.assign(array, [0, 1]);

You can also omit some indexes (and they won't be overwritten with undefined). e.g. fill just index 3 and 5:

Object.assign(array, [,,,3,,5]);

-- Sent from: mozilla.6506.n7.nabble.com/Mozilla-ECMAScript-4-discussion-f89340.html

# Isiah Meadows (7 years ago)

You could also use computed indices, too.

Object.assign(array, {
    [index]: foo,
    [index + 1]: bar,
})