OP arrays (v0.3)

From Native Big Data Documentation
Jump to: navigation, search


In op arrays are ordered lists of other objects.

Creation

In op arrays are created with the '[...]' constructor :

var s = [1,2,3,4,5]; s

Comparations

Comparation acts like other languages. The following scripts will return true

var s_1 = [1,2,3,4,5];
var s_2 = [1,2,3,4,5];
s_1 == s_2

Inheritance

OP Functions or operators that are not in an array API will be executed in every element:

This script will return [6,7,8,9,10] :

var s_1 = [1,2,3,4,5];
s_1 + 5

This script will return [1,1,1,1,1] :

var s_1 = [1,2,3,4,5];
s_1 + [0,-1,-2,-3,-4]

This script will return [2,4,6,8,10] :

var s_1 = [1,2,3,4,5];
s_1[ (a)=> { a*2 } ]

The '[]' operator

The '[]' operator in arrays is the best way to explore the data.

Getting elements

The length of an array can be obtained with the [] operator without parameters (or passing null) : The following script will return the length of the array (12):

var s_1 = [1,3,4,56];
s_1[12] = 8;
s_1

An element can be obtained as the usual way in other languages: The following scripts will return 56 ):

var s_1 = [1,3,4,56]; s_1[3]

If the [] parameter is negative then the index is couunting from the end: The following script will return 4 :

var s_1 = [1,3,4,56]; s_1[-2]

Asignation can be done as the usual way in other languages: The following scripts will return 56 ):

var s_1 = [1,3,4,56]; 
s_1[3] = 12

The [] operator with a function as parameter will execute the funcion in every element:

This script will return [2,4,6,8,10] :

var s_1 = [1,2,3,4,5];
s_1[ (a)=> { a*2 } ]

In the last case the funcion will have the following parameters: value , key and the complete array in the 'this' parameter. Chaining is possible:

This script will return [3,5,7,9,11] :

var s_1 = [1,2,3,4,5];
s_1[ (a)=> { a*2 } ] [ (a)=> { a+1 }]

If the parameter of the '[]' operator is an array then a deep get will be implemented. This example will return 6

var s_1 = [ [ 1,2 ], [3,4], [5,6], [7,8],[9,10] ];
s_1[ [ 2,1 ]    ]

Pay attention that s[ [3,4] ] is the same as s[3][4]. The difference is that s [ [3,4] ] is faster because if s don't have a 3 element s [[ 3,4] ] will return null and s [ 3 ] [4] will execute null [4] .

'[]' operator with arrays as parameters is more general. This script will return the 'c' character as a string:

var s_1 = [ [ 1,2 ], [ { 'a': 3, 'b': [ "a1", "b2", "c3"] }  ,4], [5,6], [7,8],[9,10] ];
s_1[ [ 1, 0, "b", 2, [0,1] ]  ]

This is because:

s_1[1]         == [ { 'a': 3, 'b': [ "a1", "b2", "c3"] } ,4 ]

s_1[[1,0]]     == { 'a': 3, 'b': [ "a1", "b2", "c3"] } 

s_1[[1,0,"b"]] == [ "a1", "b2", "c3"] 

s_1[[1,0,"b",2]] == "c3" 

s_1[[1,0,"b",2,[0,1]]] == 'c'

Setting elements

The push method allows adding elements to an array:

var s_1 = [ [ 1,2 ], [3,4], [5,6], [7,8],[9,10] ];
s_1.push("new element")

The '[]' operator allows this in a shorter way:

var s_1 = [ [ 1,2 ], [3,4], [5,6], [7,8],[9,10] ];
s_1[] = "new element"

s_1[] = "new element" is the same as s_1.push("new element")

The pop method allows get and remove the last element:

var s_1 = [ [ 1,2 ], [3,4], [5,6], [7,8],[9,10] ];
s_1.pop()

s_1[-1] will return the same value but without removing it. If we only want to delete the last we can write (s_1[-1] = null)

var s_1 = "Native Big Data is the fastest Big Data platform ever done";
s_1[-1]=null