Array positions in Javascript

1. Idea and proposal

I would like to have Array positions in Javascript. Array positions are based on the slice syntax proposed here

1.1 feature name

Array positions in Javascript

1.2 feature description

I would like to have more syntax in slice notation. I called this ... array of positions as it has some interesting cases with a specific slice notation

2. Use cases

use case 1: case1.js

const arr = [1, 2, 3, 4];
console.log(arr[1:3++]);// slice with operator increment ++
console.log(arr[1:3--]);// slice with operator decrement --

use case 2: case2.js

const arr = [1, 2, 3, 4];
console.log(arr[arr[0:3++], arr[0:2--]]); // slice with operator increment ++ and decrement --

use case 3: case3.js

const arr = [1, 2, 3, 4];
console.log(arr[arr[0]:arr[1], arr[0]:arr[2] ... ]);

use case 4: case4.js

const arr = [1, 2, 3, 4];
console.log(arr[0]:arr[1]);

use case 5: case5.js

const arr = [1, 2, 3, 4];
console.log(arr[[0, 1]]:arr[[1, 3]]);

use case 6: case6.js

const arr = [1, 2, 3, 4];
console.log(arr[0]:arr[3]); // 4

use case 7: case7.js

const arr = [1, 2, 3, 4];
const slice = arr[0]:arr[3]// Creating a Positional Array and Checking the Number of Elements
console.log(slice.length); // 4

Notes - use case 7:

  1. In this case we have 1 array with 4 values
  2. We are checking the number of values in this array, this array has 4 values, which shows the number 4 in the output with 'length'

use case 8: case8.js

const arr = [1, 2, 3, 4];
console.log(arr[arr[0]:arr[3]+arr[0]:arr[3]] ); //20 => 1+2+3+4+1+2+3+4=20

use case 9: case9.js

const arr = [1, 2, 3, 4];
console.log(arr[100]); // error: array with index undefined

use case 10: case10.js

const arr = [1, 2, 3, 4];
const slice = arr[arr[0]:arr[3], arr[0]:arr[3]]; // Creating a Positional Array and Checking the Number of Elements
console.log(slice.length) // 8

Notes - use case 10:

  1. In this case we have 2 arrays with 4 values
  2. We are checking the number of values in this array: 'positionalArray', this array has 8 values, which shows the number 8 in the output with 'length'
    array1 with 4 elements from index 0 to index 3 and array2 with 4 elements from index 0 to index 3

use case 11: case11.js

const arr = [1, 2, 3, 4];
const slice = arr[0]:arr[3] 
const firstElement = slice[0]
const sample1LastElement = slice[3] 
const sample2LastElement = slice[slice.length - 1]; 

Notes - use case 11:

  1. Access multiple items (indexs) from Positional Array with slice notation
  2. We have an array with 4 values and we are querying the individual index of each array
  3. By default, any value in a positional array is null.
  4. If you insert some value in the positional array, it will have the data you put, the type is dynamic/static at the same time
  5. There is no limit on the amount of elements you can allocate... there are specifications... the restricted mode and the unrestricted mode
  6. Restricted mode says you can only allocate amount of 340 elements... this is based on Excel specifications and limits
  7. Unrestricted mode works better on server side where you need to have lots of data and allocate lots of things
  8. Unrestricted mode and restricted mode will be explained next topic... Let's check the syntax of position arrays

use case 12: case12.js

'use restricted'
const arr = [1, 2, 3, 4];
console.log(arr[arr[0]:arr[3]-arr[1]:arr[2]]); // -18 => 1-2-3-4-1-2-3-4=-18

use case 13: case13.js

'use unrestricted'
const arr = [1, 2, 3, 4];
console.log(arr[arr[0]:arr[3]+arr[1]:arr[2]]); //20 => 1+2+3+4+1+2+3+4=20

use case 14: case14.js

'use restricted'
const arr = [1, 2, 3, 4];
const slice = arr[0]:arr[1000] // Creating a Positional Array and Checking the Number of Elements
console.log(slice.length); // error: array with index undefined, slice in restricted mode, limit: 340 values 

3. Limits for Array positions

  1. There is no limit on the amount of elements you can allocate... there are specifications... the restricted mode and the unrestricted mode as example: 'use unrestricted'
  2. Restricted mode says you can only allocate amount of 340 elements... this is based on Excel as example: 'use restricted'
  3. Unrestricted mode or Restricted mode is based on modules-pragma and module-blocks and built-in-modules

4. Use cases/Main objectives/Reasons for Array positions from slice notation(Benefits for Community):

  1. You need special operations on internal unstructured Javascript data
  2. You don't want to use excel, you want to code in that case you would use positional arrays
  3. You need to create certain filters and conditions for large collections of data
  4. You don't want to use a third-party library, you want to have something real in the language without relying on a third-party library
  5. Is possible migrate people who like excel to Javascript, because positional arrays are row and column oriented
  6. As far as I know there is no struct in Javascript, this could allow custom struct in Javascript through positional arrays
  7. This would be similar to struct with pointer, but is similar to excel too
  8. It is based on the R language for Javascript

5. Requirements/Problems

  1. Positional array requires reactive programming, I know this because Excel works with reactive programming. Here are some examples of libraries that do reactive programming: rxjs
  2. You must have knowledge in R or Excel to understand how to use positions arrays
  3. Keywords: "pos_array" or "positionalArray" or "pa" - I still haven't thought of the name or keyword
  4. There is the proposal slice notation. But slice notation It doesn't have those requirements I need.

6. Syntax/Requirements

case 6.1

positionalArray[index]:positionalArray[index] 

case 6.2

positionalArray[array[index]:array[index], array[index]:array[index] ... ]  

case 6.3

positionalArray[[index_start, index_end]]:positionalArray[[index_start, index_end]]

case 6.4

positionalArray[index_start]>=positionalArray[index_start] 

case 6.5

positionalArray[index_start]<=positionalArray[index_start] 

case 6.6

positionalArray[index_start, index_end]:positionalArray[index_start]

case 6.7

positionalArray[index_start, index_end]:positionalArray[index_start, index_end]

7. Areas of use for positional arrays

"Big data", "Statistic", "Small, medium and large companies", "unstructured data"

8. "Features" of positional arrays with slice notation

  1. Can I limit the amount of bits or bytes in each positional array for better performance
  2. Positional arrays are interactive
  3. Can I remove multiple individual values in Javascript
  4. Easily change different positions in different arrays
  5. "You can use async and ternary comparison assignment operators with positional array"

8.5 - case: "You can use async and ternary comparison assignment operators with positional array": specialcase.js

async function serviceEmail() {
  const arr = [1, 2, 3, 4];
  return arr[0]:arr[3];  //I have 4 positional elements with value null
}

console.log(serviceEmail()); // 4 

// If i need to reorder or expand arrays i could with positional array... here:
let reorderElementService = arr[arr[3]:arr[0], serviceEmail()].reorder() // here I reordered the values
console.log(reorderElementService); // 4

var status = (reorderElementService == serviceEmail) ? 'equal' : 'not equal';
console.log(status);

Notes - case 8.5:

The amount of elements is the same, but the order has been changed by index

9. More notes

  1. I hope to help the community
  2. If this is not interesting I apologize
  3. There are functional and event-driven libraries as ramda, things like Excel don't seem to be as fun as programming with Javascript.
  4. I would like to have a syntax for the positional array to avoid Excel as well as to solve specific problems in multiple arrays
  5. Libraries like exceljs, js-xlsx, sheetjs have positional arrays
  6. I read about Global_Objects/Array here
  7. It would be nice to have something like positional arrays, this is reminiscent of the slice notation proposal and also a bit of the pandas Python library that handles a huge volume of data with array
  8. Language like R works a lot with arrays, it would be nice to have positional arrays because it is close to R and this can make many developers like Javascript because it looks like R and a little Excel
  9. Database like redis works exclusively or at least largely with arrays, having native support for positional arrays in Javascript this can help web developers make interesting code close to what you can use with Redis
  10. Positional arrays use slice notation
  11. Positional arrays are interactive, here are some views on that iterative-assignment
  12. Languages like Ocaml and Haskell allow you to manage arrays in a very interesting and abstract way. Here is an example in Ocaml
  13. If there is such proposal accepted built-in-module-for-array-oriented-math-functions - Part of positional array will be partially accepted ... Because can benefit from built-in math functions, as in the case of my proposal is to make users from Excel to Javascript this could help
  14. My idea in short is this slice notation with more syntax

8. Main cool, interesting things

  1. Has 'restricted mode' or 'unrestricted mode' directive to limit or delimit the amount of data within an array
  2. Has the syntax like something you know like Excel
  3. It should be a great resource to help with functional or event-driven programming programming
  4. The coolest thing with positional array is the complex operations on different arrays

10. Interesting cases

case 1 with array positions(more syntax in slice notation)

const arr = [1, 2, 3, 4, 20, 30];
console.log( arr[1:3>=10]); // return a new array with the value that is greater than 10
// → ['30', '20']

console.log( arr[1:3<10]); // return a new array with the value that is less than 10
// → ['1', '2', '3', '4']

case 2: Built-in module for array-oriented math functions and Array positions

const arr = [1, 2, 3, 4, 20, 30];
arr[1:3].greater // return array with elements: 30
arr[1:3].less // return array with elements: 1
arr[1:3].float // return value or false
arr[1:3].double  // return value or false
arr[1:3].string // return value or false
arr[1:3].greater(10) // return array with elements: 30 // arr[1:3]>10
arr[1:3].less(10) // return array with elements: 1 // arr[1:3]<10
arr[1:3]++ // auto increment
arr[1:3]-- // auto decrement
arr[1:3].sum
arr[1:3].push
arr[1:3].pop
arr[1:3].min
arr[1:3].median
arr[1:3].count
arr[1:3].null
arr[1:3].equal(10)
arr[1:3].empty("")
arr[1:3].integer
arr[1:3].boolean
arr[1:3].orderBy
arr[1:3].reorder
arr[1:3].index

Notes

  1. Are just ideas, not something that will be done 100%

@theScottyJam How are you? What do you think of this idea? I had an idea... I want to know your opinion!

Arrays have a slice method.

1 Like

@ljharb what do you think of this idea? is something bad? ... wanted to do multiple operations with different arrays

i think your idea is just syntax for slice, and your specific use case wouldn’t come close to meeting the bar for syntax.

See GitHub - tc39/proposal-slice-notation for a more general solution.

1 Like

@ljharb Hi!

  1. What you said is truth. Yes, it's the slice idea but with "more syntax"
  2. I fixed the post, I think it's better, more explanatory... please check it here.

Could you add example outputs to your examtples (e.g. state what the console.logs actually log out)? That would make it easier to understand what you're going for (instead of us trying to guess as to what this syntax is supposed to do).

@theScottyJam Hi! How are you? :sweat_smile: So... I would like the slice syntax and excel syntax. What I propose here is slice syntax with excel syntax.

  1. I requested this as I would like to do multiple array operations with just the slice syntax.
  2. The slice syntax doesn't support some things I need to do.
  3. I found it interesting to talk about the proposal as "array positions"
  4. If you take a look at the proposed slice syntax, you will notice that it is not possible to perform a series of operations with the slice syntax. My idea would be to have this possibility
  5. For example, I can be more consistent in certain cases like this
const arr = [1, 2, 3, 4, 20, 30];
console.log( arr[1:3>=10]); // return a new array with the value that is greater than 10
// → ['30', '20'] new array with values from arr

@ptomato What do you think of this idea?

@theScottyJam I found a better title: "Mathematical operations with Syntax Slice" ... Really the title 'array positions in javascript' is very confusing ... the best would be: "Mathematical operations with Syntax Slice"

@theScottyJam @ljharb some examples ...

example1.js

const arr = [1, 2, 3, 4, 20, 30];
console.log( arr[1:3>=10]); // return a new array with the value that is greater than 10
// → ['30', '20'] new array with values from arr

example2.js

const arr = [1, 2, 3, 4, 20, 26, 30];0, 1, 2, 3
var beverage = (arr[1:3>=21]) ? "true": "false";
console.log(beverage); // "false"
 // 1:3 is arr[1]= 2 >= 21 is false, arr[2]= 3 >=21 is false, arr[3]= 4 >=21  is false

example3.js

const arr = [1, 2, 3, 4, 20, 26, 30];
var beverage = (arr.greater>=21) ? "true": "false"; // greater = 30
console.log(beverage); // "true"

Example 1: arr.filter(x => x >= 10)

Example 2: arr.some(x => x >= 21)

Example 3: arr.greater would already be a property on arrays, so this is a nonstarter, but: arr.some(x => x >= 21) again.

2 Likes