Add an easier method for slicing and stepping strings and arrays

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

Add an easier method for slicing and stepping strings and arrays

Alexander Craggs
I always found the Python methodology of selecting characters from strings (or items from arrays) to be the most natural I've found among a number of language solutions.  Was considering whether adding the syntax would be beneficial for JavaScript.  For those who do not know, the syntax is as so:

```js
const str = 'example'
const arr = ['e', 'x', 'a', 'm', 'p', 'l', 'e']
// Single elements are selected as in JS:
str[1] // 'x'
arr[2] // 'a'
// Multiple elements are selected by providing two numbers, delimited by a colon
// The first number is inclusive, the last number is exclusive
str[1:3] // 'xa'
arr[3:6] // ['m', 'p', 'l']
// These numbers can also be negative, resulting in indexing from the end of the string/array
str[-1] // 'e'
str[-3:-1] // 'pl'
arr[-5:-3] // ['a', 'm']
arr[0:-5] // ['e', 'x']
// There is also a third number, which steps through the selection chosen by the first two
str[0:5:2] // 'eap'
// This number can be negative, which results in stepping backwards through the string/array
str[::-1] // 'elpmaxe'
// An empty item represents one of the following based upon it's position: [0:length:1]
```

The most official documentation I could find on this topic is from the Python tutorial, a more useful reference however might be the Stack Overflow post on the topic.

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

Re: Add an easier method for slicing and stepping strings and arrays

Alexander Craggs
In which case,  perhaps change this proposal to .slice(start, end, step)

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

Re: Add an easier method for slicing and stepping strings and arrays

Isiah Meadows-2

That, I could support.


On Wed, Jul 26, 2017, 16:38 Alexander Craggs <[hidden email]> wrote:
In which case,  perhaps change this proposal to .slice(start, end, step)
_______________________________________________
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: Add an easier method for slicing and stepping strings and arrays

Isiah Meadows-2

To clarify, I'm just a normal, non-committee person. So that's a "I like the idea and it looks workable", but not "I'll champion this for you" (when I can't).


On Wed, Jul 26, 2017, 21:41 Isiah Meadows <[hidden email]> wrote:

That, I could support.


On Wed, Jul 26, 2017, 16:38 Alexander Craggs <[hidden email]> wrote:
In which case,  perhaps change this proposal to .slice(start, end, step)
_______________________________________________
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