Multi-index assignment.

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Multi-index assignment.

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

For example,

```js
array[0] = 1
array[1] = 2
```

could be written as 

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

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

```js
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]
```

/#!/JoePea

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Multi-index assignment.

Jordan Harband
Can you elaborate on these situations?

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

On Mon, Dec 4, 2017 at 11:21 AM, /#!/JoePea <[hidden email]> wrote:
I always run into situations where I'd like to assign some things from another array.

For example,

```js
array[0] = 1
array[1] = 2
```

could be written as 

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

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

```js
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]
```

/#!/JoePea

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Multi-index assignment.

T.J. Crowder-2
In reply to this post by /#!/JoePea
On Mon, Dec 4, 2017 at 7:21 PM, /#!/JoePea <[hidden email]> wrote:

>
> I always run into situations where I'd like to assign some things from another array.
>
> ...
>
> could be written as
>
> ```js
> 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:

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

-- T.J. Crowder
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Multi-index assignment.

Alex Vincent
In reply to this post by /#!/JoePea
---------- Forwarded message ----------
From: Jordan Harband <[hidden email]>
To: "/#!/JoePea" <[hidden email]>
Cc: es-discuss <[hidden email]>
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.
--
"The first step in confirming there is a bug in someone else's work is confirming there are no bugs in your own."
-- Alexander J. Vincent, June 30, 2001

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Multi-index assignment.

medikoo
In reply to this post by /#!/JoePea
Just do:

```js
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:

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




--
Sent from: http://mozilla.6506.n7.nabble.com/Mozilla-ECMAScript-4-discussion-f89340.html
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Multi-index assignment.

Isiah Meadows-2

You could also use computed indices, too.

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


On Tue, Dec 5, 2017, 06:39 medikoo <[hidden email]> wrote:
Just do:

```js
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:

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




--
Sent from: http://mozilla.6506.n7.nabble.com/Mozilla-ECMAScript-4-discussion-f89340.html
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss