The arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a array nor the types of its elements are fixed. The arrays are used to store multiple values in a single variable.
1 Creating an Array
create an ARRAY | |
---|---|
Operador Xsql | <array name='name'> |
Syntax | var mArrName = [value1, value2, ...]; |
Return | An array. |
<xsql-script> <body> <array name='cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <println>Cities array : <cities/></println> </body> </xsql-script>
Cities array : [Barcelona,Paris,Roma]
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Cities array : ${mArrayCities}`);
Cities array : Barcelona,Paris,Roma
Spaces and line breaks are not important. A declaration can span multiple lines.
var mArrayCountries = [ 'Portugal', 'España', 'Alemania' ]; console.log(`Countries array : ${mArrayCountries}`);
Countries array : Portugal,España,Alemania
2 Access an Array
Access to an array element is possible by referring to the index number.
access an ARRAY | |
---|---|
Operador Xsql | <array.get name='name'> |
Syntax | var mValue = Array[i]; |
Return | Accesses the value. |
<xsql-script> <body> <array name='cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> <string>Budapest</string> </array> <set name='m_value1'> <array.get name='cities'> <number>0</number> </array.get> </set> <set name='m_value2'> <array.get name='cities'> <number>2</number> </array.get> </set> <println>First element : [<m_value1 />]</println> <println>Third element : [<m_value2 />]</println> </body> </xsql-script>
First element : [Barcelona]
Third element : [Roma]
var mArrayCities = ['Barcelona','Paris','Roma','Budapest']; var mStrValue1 = mArrayCities[0]; var mStrValue2 = mArrayCities[2]; console.log(`First element : [${mStrValue1}]`); console.log(`Third element : [${mStrValue2}]`);
First element : [Barcelona]
Third element : [Roma]
The first element of an array is at index 0, and the last element is at the index value equal to the value of the array's length property minus 1. For example, [0] is the first element, and [1] is the second element.
3 Changing element at an Array
change element at an ARRAY | |
---|---|
Operador Xsql | <array.set name='name'> |
Syntax | Array[i] = mValue; |
Return | Changed element. |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <println>Array cities before change: <m_cities /></println> <array.set> <m_cities/> <number>1</number> <string>Budapest</string> </array.set> <println>Array cities after change: <m_cities /></println> </body> </xsql-script>
Array cities before change: [Barcelona,Paris,Roma]
Array cities after change: [Barcelona,Budapest,Roma]
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Array cities before change: : ${mArrayCities}`); mArrayCities[1] = 'Budapest'; console.log(`Array cities after change: : ${mArrayCities}`);
Array cities before change: : Barcelona,Paris,Roma
Array cities after change: : Barcelona,Budapest,Roma
4 Loop over an Array
Using a for loop is the safest way to loop through an array.
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <set name='m_length'> <array.size name='m_cities' /> </set> <set name='m_length'> <sub> <m_length/> 1 </sub> </set> <for name='i' start='0' end='${m_length}' step='1'> <do> <set name='m_city'> <array.get name='m_cities'> <number><i/></number> </array.get> </set> <println>Array element [<i/>] : <m_city/></println> </do> </for> </body> </xsql-script>
Array element [0] : Barcelona
Array element [1] : Paris
Array element [2] : Roma
var mArrayCities = ['Barcelona','Paris','Roma']; var mIntLength = mArrayCities.length; for (i = 0; i < mIntLength; i++) { console.log(`Array element[${i}] : ${mArrayCities[i]}`) }
Array element[0] : Barcelona
Array element[1] : Paris
Array element[2] : Roma
var mArrayCities = ['Barcelona','Paris','Roma']; mArrayCities.forEach(function(item, index, array) { console.log(`Array element[${index}] : ${item}`) })
Array element[0] : Barcelona
Array element[1] : Paris
Array element[2] : Roma
5 Clear an array
Deletes all the elements of an array.
clear an ARRAY | |
---|---|
Operador Xsql | <array.clear name='array_name' /> |
Syntax | mArrName = [] |
Return | Clean array. |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <println>Array cities before of clears elements: <m_cities /></println> <array.clear name='m_cities' /> <println>Array cities after of clears elements: <m_cities /></println> </body> </xsql-script>
Array cities before of clears elements: [Barcelona,Paris,Roma]
Array cities after of clears elements: []
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Array cities before of clears elements: ${mArrayCities}`); mArrayCities = []; console.log(`Array cities after of clears elements: ${mArrayCities}`);
Array cities before of clears elements: Barcelona,Paris,Roma
Array cities after remove element:
6 Properties
6.1 Array.length
The length property returns the number of elements in that array.
length PROPERTY | |
---|---|
Operador Xsql | <array.size name='name'> |
Syntax | mArrayName.length |
Parameters |
No parameters. |
Return | A Number, representing the number of elements in the array object |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <set name='m_result'> <array.size name='m_cities' /> </set> <println>length cities array: <m_result /></println> </body> </xsql-script>
length cities array: 3
var mArrayCities = ['Barcelona','Paris','Roma']; var mIntResult = mArrayCities.length; console.log(`length cities array: ${mIntResult}`);
length cities array:: 3
Length
The value of the length property is an integer with a positive sign and a value less than 2 to the 32nd power (232).7 Methods
7.1 Array.concat()
The concat() method is used to merge two or more arrays.
concat() METHOD | |
---|---|
Syntax | var mArrayNew = mArrayOld.concat([value1[, value2[, ...[, valueN]]]]) |
Parameters | Arrays and/or values to concatenate into a new array. |
Return | A new Array. |
The concat() method does not change the existing arrays, but instead returns a new array.
- Concatenating two arrays
<xsql-script> <body> <set name='m_cities1'> <array> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> </set> <set name='m_cities2'> <array> <string>Londres</string> <string>Lisboa</string> <string>Berlín</string> </array> </set> <set name='m_all_Cities'> <array.clone name='m_cities1' /> </set> <iterator name='m_city2'> <in> <m_cities2/> </in> <do> <array.add name='m_all_Cities'> <m_city2 /> </array.add> </do> </iterator> <println>Elements from array Cities1: <m_cities1 /></println> <println>Elements from array Cities2: <m_cities2 /></println> <println>Join of the two arrays: <m_all_Cities /></println> </body> </xsql-script>
Elements from array Cities1: [Barcelona,Paris,Roma]
Elements from array Cities2: [Londres,Lisboa,Berlín]
Join of the two arrays: [Barcelona,Paris,Roma,Londres,Lisboa,Berlín]
var mArrayCities = ['Barcelona','Paris','Roma']; var mArrayCities2 = ['Londres','Lisboa', 'Berlin']; var mArrAllCities = mArrayCities.concat(mArrayCities2); console.log(`Cities array: ${mArrayCities}`); console.log(`Cities2 array: ${mArrayCities2}`); console.log(`Join two arrays: ${mArrAllCities}`);
Cities array: Barcelona,Paris,Roma
Cities2 array: Londres,Lisboa,Berlin
Join two arrays: Barcelona,Paris,Roma,Londres,Lisboa,Berlin
- Concatenating three arrays
<xsql-script> <body> <set name='m_cities1'> <array> <string>Barcelona</string> <string>Sevilla</string> <string>Valencia</string> </array> </set> <set name='m_cities2'> <array> <string>Roma</string> <string>Florencia</string> <string>Venecia</string> </array> </set> <set name='m_cities3'> <array> <string>Lisboa</string> <string>Oporto</string> <string>Aveiro</string> </array> </set> <println>Elements from array Cities1: <m_cities1 /></println> <println>Elements from array Cities2: <m_cities2 /></println> <println>Elements from array Cities3: <m_cities3 /></println> <set name='m_all_Cities'> <array.clone name='m_cities1' /> </set> <for name='i' start='2' end='3' step='1'> <do> <iterator name='m_cities${i}'> <in> <get name='m_cities${i}'/> </in> <do> <array.add name='m_all_Cities'> <get name='m_cities${i}'/> </array.add> </do> </iterator> </do> </for> <println>Join of the three arrays: <m_all_Cities /></println> </body> </xsql-script>
Elements from array Cities1: [Barcelona,Sevilla,Valencia]
Elements from array Cities2: [Roma,Florencia,Venecia]
Elements from array Cities3: [Lisboa,Oporto,Aveiro]
Join of the three arrays: [Barcelona,Sevilla,Valencia,Roma,Florencia,Venecia,Lisboa,Oporto,Aveiro]
var mArrayCitiesEs = ['Barcelona','Sevilla','Valencia']; var mArrayCitiesIt = ['Roma','Florencia','Venecia']; var mArrayCitiesPt = ['Lisboa','Oporto','Aveiro']; var mArrAllCities = mArrayCitiesEs.concat(mArrayCitiesIt, mArrayCitiesPt); console.log(`Spain cities array: ${mArrayCitiesEs}`); console.log(`Italy cities array: ${mArrayCitiesIt}`); console.log(`Portugal cities array: ${mArrayCitiesPt}`); console.log(`Join all arrays: ${mArrAllCities}`);
Spain cities array: Barcelona,Sevilla,Valencia
Italy cities array: Roma,Florencia,Venecia
Portugal cities array: Lisboa,Oporto,Aveiro
Join all arrays: Barcelona,Sevilla,Valencia,Roma,Florencia,Venecia,Lisboa,Oporto,Aveiro
- Concatenating values to an array
var mArrayCities = ['Barcelona','Roma']; var mArrAllCities = mArrayCities.concat('Berlin', ['Paris','Oporto']); console.log(`Cities array: ${mArrayCities}`); console.log(`Join array with values: ${mArrAllCities}`);
Cities array: Barcelona,Roma
Join array with values: Barcelona,Roma,Berlin,Paris,Oporto
- Concatenating nested arrays
var mArrayCities = [['Roma']]; var mArrayCities2 = ['Barcelona',['Berlin', 'Valencia']]; var mArrAllCities = mArrayCities.concat(mArrayCities2); console.log(`Cities array: ${mArrayCities}`); console.log(`Cities array2: ${mArrayCities2}`); console.log(`Join array with values: ${mArrAllCities}`);
Cities array: Roma
Cities array2: Barcelona,Berlin,Valencia
Join array with values: Roma,Barcelona,Berlin,Valencia
Parameters
If all parameters are omitted, concat returns a shallow copy of the existing array on which it is called.7.2 Array.entries()
The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
entries() METHOD | |
---|---|
Syntax | array.entries() |
Parameters | No parameters. |
Return | A new Array iterator object. |
This method does not change the original array.
var mArrFruits = ['pineapple', 'apple', 'banana', 'orange']; var mIterator = mArrFruits.entries(); console.log(mIterator.next().value); console.log(mIterator.next().value); console.log(mIterator.next().value); console.log(mIterator.next().value);
[0, pineapple]
[1, apple]
[2, banana]
[3, orange]
- Using a for…of loop
<xsql-script> <body> <set name='m_Fruits'> <array> <string>Pinaple</string> <string>Apple</string> <string>Banana</string> <string>Orange</string> </array> </set> <struct.declare type='oEntry'> <field name='key' type='integer' /> <field name='value' type='string' /> </struct.declare> <set name="m_Iterator"><array /></set> <set name="m_counter">0</set> <iterator name="m_fruit"> <in> <m_Fruits /> </in> <do> <set name='m_row'> <struct type='oEntry'> <get name='m_counter'/> <get name='m_fruit'/> </struct> </set> <array.add name='m_Iterator'> <m_row /> </array.add> <set name='m_counter'> <add> <m_counter/> 1 </add> </set> </do> </iterator> <iterator name="m_itera"> <in> <m_Iterator /> </in> <do> <println> [<m_itera.key /> , <m_itera.value />] </println> </do> </iterator> </body> </xsql-script>
[0 , Pinaple]
[1 , Apple]
[2 , Banana]
[3 , Orange]
var mArrFruits = ['pineapple', 'apple', 'banana', 'orange']; var mIterator = mArrFruits.entries(); for (let mFruit of mIterator) { console.log(mFruit); }
[0, pineapple]
[1, apple]
[2, banana]
[3, orange]
7.3 Array.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
filter() METHOD | |
---|---|
Syntax | mArrayName.filter(function(currentValue, index, array), thisArg) |
Parameters |
function: to be run for test each element of the array. Return a true value to keep the element, or false otherwise. currentValue: The current element being processed in the array. index: The index of the current element being processed in the array. array: The array filter was called. thisArg: Value to use as this when executing function. |
Return | A new array with the elements that pass the test. If no elements pass the test, an empty array will be returned. |
var mArrWords = ['gift', 'limit', 'cards', 'exuberant', 'destruction', 'future']; function __checkWord(pStrWord) { if (pStrWord.length > 6){ return pStrWord; } } let mArrResult = mArrWords.filter(__checkWord); console.log(`Result array : ${mArrResult}`);
Result array : exuberant,destruction
7.4 Array.forEach()
The forEach() method executes a provided function once for each array element.
forEach() METHOD | |
---|---|
Syntax | mArrayName.forEach(function(currentValue, index, array),thisArg) |
Parameters |
function: to execute on each element. currentValue: the current element being processed in the array index: the index of currentValue in the array. array: the array forEach() was called upon. thisArg: Value to use as this when executing function. |
Return | Undefined. |
forEach() does not mutate the array on which it is called. (However, function may do so).
function __checkAges(pIntAge) { if (pIntAge > 18) { console.log(`Age old: ${pIntAge}`); } } var mArrAges = [45, 4, 9, 16, 25]; mArrAges.forEach(__checkAges);
Age old: 45
Age old: 25
7.5 Array.indexOf()
The indexOf() method searches the first occurrence of an element value,that is passed as an argument, at an array and returns its position.
indexOf() METHOD | |
---|---|
Operador Xsql | <array.indexOf name='array_name' > |
Syntax | mArrayName.indexOf(item, start) |
Parameters |
item: element to locate in the array. start: Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the end. |
Return | The first index of the element in the array; -1 if not found. |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> <string>Paris</string> </array> <set name='m_position'> <array.indexOf name='m_cities'> <string>Paris</string> </array.indexOf> </set> <println>Paris position: [<m_position />]</println> </body> </xsql-script>
Paris position: [1]
var mArrayCities = ['Barcelona','Paris','Roma','Paris']; var mIntPosition = null; mIntPosition = mArrayCities.indexOf('Paris'); console.log(`Paris position: [${mIntPosition}]`);
Paris position: [1]
7.6 Array.isArray()
The Array.isArray() method determines whether the passed value is an Array.
isArray() METHOD | |
---|---|
Syntax | Array.isArray(value) |
Parameters |
value: the value to be checked. |
Return | A Boolean. Returns true if the value is an Array; otherwise, false. |
var mArrNumbers = [2,3,5,7,11]; var mStrNumbers = 'two, three, five'; var mStrValue = null; console.log(`mArrNumbers is array: [${Array.isArray(mArrNumbers)}]`); console.log(`mStrNumbers is array: [${Array.isArray(mStrNumbers)}]`); console.log(`mStrValue is array: [${Array.isArray(mStrValue)}]`);
mArrNumbers is array: [true]
mStrNumbers is array: [false]
mStrValue is array: [false]
7.7 Array.join()
The join() method creates and returns a new string by concatenating all of the elements in an array, separated by commas or a specified separator string.
indexOf() METHOD | |
---|---|
Operador Xsql | <array.join separator='separator' > |
Syntax | mArrayName.join(separator) |
Parameters |
separator: Specifies a string to separate each pair of adjacent elements of the array. If omitted, the array elements are separated with a comma (","). |
Return | A string with all array elements joined. |
If separator is an empty string, all elements are joined without any characters in between them.
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <set name='m_result'> <array.join separator=' - '> <m_cities /> </array.join> </set> <println>Cities: <m_result /></println> </body> </xsql-script>
Cities: Barcelona - Paris - Roma
var mArrayCities = ['Barcelona','Paris','Roma']; var mStrResult = mArrayCities.join(' - '); console.log(`Cities: ${mStrResult}`);
Cities: Barcelona - Paris - Roma
Element
If an array element is undefined, null or an empty array [], it is converted to an empty string.7.8 Array.keys()
The keys() method returns a new Array Iterator object that contains the keys for each index in the array.
keys() METHOD | |
---|---|
Syntax | mArrayName.keys() |
Parameters |
No parameters. |
Return | A new Array iterator object. |
<xsql-script> <body> <set name='m_cities'> <array> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> </set> <set name='m_length'> <array.size name='m_cities' /> </set> <set name='m_length'> <sub> <m_length/> 1 </sub> </set> <set name='m_iterator'> <array/> </set> <for name='i' start='0' end='${m_length}' step='1'> <do> <array.add name='m_iterator'> <i /> </array.add> <println>Key : [<i/>] </println> </do> </for> </body> </xsql-script>
Key : [0]
Key : [1]
Key : [2]
var mArrayCities = ['Barcelona','Paris','Roma']; const mIterator = mArrayCities.keys(); for (const key of mIterator) { console.log(`key : [${key}] `); }
key : [0]
key : [1]
key : [2]
7.9 Array.lastIndexOf()
The lastIndexOf() method searches the last occurrence of an item value,that is passed as an argument, at an array and returns its position.
lastIndexOf() METHOD | |
---|---|
Syntax | mArrayName.lastIndexOf(item, start) |
Parameters |
item: item to locate in the array. start: Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the beginning |
Return | The last index of the element in the array; -1 if not found. |
<xsql-script> <body> <set name='m_cities'> <array> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> <string>Paris</string> </array> </set> <set name='m_city_to_search'> <string>Paris</string> </set> <set name='m_position' type="integer">0</set> <set name='m_length'> <array.size name='m_cities' /> </set> <set name='m_length'> <sub> <m_length/> 1 </sub> </set> <for name='i' start='0' end='${m_length}' step='1'> <do> <set name='m_city'> <array.get name='m_cities'> <number><i/></number> </array.get> </set> <if> <expr><eq> <m_city /><m_city_to_search /></eq></expr> <then> <set name='m_position'> <i/> </set> </then> </if> </do> </for> <println>Paris position: [<m_position />]</println> </body> </xsql-script>
Paris position: [3]
var mArrayCities = ['Barcelona','Paris','Roma','Paris']; var mIntPosition = null; mIntPosition = mArrayCities.lastIndexOf('Paris'); console.log(`Paris position: [${mIntPosition}]`);
Paris position: [3]
7.10 Array.map()
The map() method creates a new array of the results of calling a provided function on every element in the calling array.
map() METHOD | |
---|---|
Syntax | mArrayName.map(function(currentValue, index, array),thisArg) |
Parameters |
function: is called for every element of array, the returned value is added to newArray. currentValue: the current element being processed in the array. index: the index of the current element being processed in the array. array: the array map was called upon. thisArg: Value to use as this when executing function. |
Return | A new array with each element being the result of calling function |
function __lengthWord(word) { return word.length; } var mArrWords = ['gift', 'limit', 'cards', 'exuberant', 'destruction', 'future']; const mArrResult = mArrWords.map(__lengthWord); console.log(`Original array : ${mArrWords}`); console.log(`Result array : ${mArrResult}`);
Original array : gift,limit,cards,exuberant,destruction,future
Result array : 4,5,5,9,11,6
7.11 Array.pop()
The pop() method removes the last element from an array and returns that element.
The pop() method changes the length of the array.
pop() METHOD | |
---|---|
Operador Xsql | <array.removeLast name='array_name' /> |
Syntax | mArrayName.pop() |
Parameters | No parameters. |
Return | The removed element from the array |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <println>Array cities before remove element: <m_cities /></println> <array.removeLast name='m_cities' /> <println>Array cities after remove element: <m_cities /></println> </body> </xsql-script>
Array cities before remove element: [Barcelona,Paris,Roma]
Array cities after remove element: [Barcelona,Paris]
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Array cities before remove element: ${mArrayCities}`); mArrayCities.pop(); console.log(`Array cities after remove element: ${mArrayCities}`);
Array cities before remove element: Barcelona,Paris,Roma
Array cities after add element: Barcelona,Paris
7.12 Array.push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
The push() method changes the length of the array.
push() METHOD | |
---|---|
Operador Xsql | <array.add name='array_name' /> |
Syntax | mArrayName.push(element1, element2, ..., elementX) |
Parameters | The element(s) to add to the end of the array. |
Return | A Number, representing the new length of the array |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <println>Array cities before add element: <m_cities /></println> <array.add name='m_cities'> <string>Budapest</string> </array.add> <println>Array cities after add element: <m_cities /></println> </body> </xsql-script>
Array cities before add element: [Barcelona,Paris,Roma]
Array cities after add element: [Barcelona,Paris,Roma,Budapest]
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Array cities before add element: ${mArrayCities}`); var mIntLength = mArrayCities.push('Budapest'); console.log(`Array cities after add element: ${mArrayCities}`); console.log(`New length of array : ${mIntLength}`);
Array cities before add element: Barcelona,Paris,Roma
Array cities after add element: Barcelona,Paris,Roma,Budapest
New length of array : 4
7.13 Array.reduce()
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.
The reduce() method does not change the original array.
reduce() METHOD | |
---|---|
Syntax | mArrayName.reduce(function(accumulator, currentValue, index, array),initialValue) |
Parameters |
function: to execute on each element in the array (except for the first, if no initialValue is supplied). accumulator: the accumulator accumulates return values of the function. currentValue: the current element being processed in the array. index: the index of the current element being processed in the array. array: the array reduce() was called upon. initialValue:a value to use as the first argument to the first call of the function. |
Return | Returns the accumulated result from the last call of the function |
var mArraNumbers = [1, 2, 3, 4]; function __functionAdd(accumulator, currentValue) { return accumulator + currentValue; } console.log(`1 + 2 + 3 + 4 = ${mArraNumbers.reduce(__functionAdd)}`); console.log(`5 + 1 + 2 + 3 + 4 : ${mArraNumbers.reduce(__functionAdd, 5)}`);
1 + 2 + 3 + 4 : 10
5 + 1 + 2 + 3 + 4 : 15
7.14 Array.reduceRight()
The reduceRight() method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value
reduce() METHOD | |
---|---|
Syntax | mArrayName.reduceRight(function(accumulator, currentValue, index, array),initialValue) |
Parameters |
function: to execute on each value in the array. accumulator: the value previously returned in the last invocation of the function. currentValue: the current element being processed in the array. index: the index of the current element being processed in the array. array: the array reduceRight() was called upon. initialValue:a value to use as the first argument to the first call of the function. |
Return | Returns the accumulated result from the last call of the function |
var mArraNumbers = [[0, 1], [2, 3], [4, 5]]; var mArrResult = [[0, 1], [2, 3], [4, 5]].reduceRight( (accumulator, currentValue) => accumulator.concat(currentValue) ); console.log(`mArraNumbers : ${mArraNumbers}`); console.log(`mArrResult : ${mArrResult}`);
mArraNumbers : 0,1,2,3,4,5
mArrResult : 4,5,2,3,0,1
7.15 Array.reverse()
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
The reverse() this method will change the original array.
reverse() METHOD | |
---|---|
Operador Xsql | <array.reverse name='array_name' /> |
Syntax | mArrayName.reverse() |
Parameters | No parameters. |
Return | The reversed array. |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <println>Array cities before reverse: <m_cities /></println> <array.reverse name='m_cities' /> <println>Array cities after reverse: <m_cities /></println> </body> </xsql-script>
Array cities before reverse: [Barcelona,Paris,Roma]
Array cities after reverse: [Roma,Paris,Barcelona]
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Array cities before reverse: ${mArrayCities}`); mArrayCities.reverse(); console.log(`Array cities after reverse: ${mArrayCities}`);
Array cities before reverse: Barcelona,Paris,Roma
Array cities after reverse: Roma,Paris,Barcelona
7.16 Array.shift()
The shift() method removes the first element from an array and returns that removed element.
The shift() method changes the length of the array.
shift() METHOD | |
---|---|
Operador Xsql | <array.removeFirst name='array_name' /> |
Syntax | mArrayName.shift() |
Parameters | No parameters. |
Return | The removed element from the array. |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <println>Array cities before remove element: <m_cities /></println> <array.removeFirst name='m_cities' /> <println>Array cities after remove element: <m_cities /></println> </body> </xsql-script>
Array cities before remove element: [Barcelona,Paris,Roma]
Array cities after remove element: [Paris,Roma]
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Array cities before remove element: ${mArrayCities}`); mArrayCities.shift(); console.log(`Array cities after remove element: ${mArrayCities}`);
Array cities before remove element: Barcelona,Paris,Roma
Array cities after remove element: Paris,Roma
7.17 Array.slice()
The slice() method returns a copy of a portion of an array into a new array selected from start to end (end not included) where start and end represent the index of items in that array.
slice() METHOD | |
---|---|
Operador Xsql | <array.clone /> |
Syntax | mArrayName.slice(start, end) |
Parameters |
start: Optional. An integer that specifies where to start the selection. end: Optional. An integer that specifies where to end the selection. |
Return | A new array containing the extracted elements. |
The slice() method not remove any elements from the source array, it does creates a new array.
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <set name='m_cities_clone'> <array.clone name='m_cities' /> </set> <println>Original array: <m_cities /></println> <println>Copie array: <m_cities_clone /></println> </body> </xsql-script>
Original array: [Barcelona,Paris,Roma]
Copie array: [Barcelona,Paris,Roma]
var mArrayCities = ['Barcelona','Paris','Roma']; var mArrCitiesClone = mArrayCities.slice(); console.log(`Original array: ${mArrayCities}`); console.log(`Copied array: ${mArrCitiesClone}`);
Original array: Barcelona,Paris,Roma
Copied array: Barcelona,Paris,Roma
Start is optional:
- If start is undefined, slice starts from the index 0.
- If start is greater than the index range of the sequence, an empty array is returned.
- If end is omitted, slice extracts through the end of the sequence (array.length).
- If end is greater than the length of the sequence, slice extracts through to the end of the sequence (array.length).
Copy a portion of an existing array
var mArrayCities = ['Barcelona','Paris','Roma','Budapest']; var mArrCitiesClone = mArrayCities.slice(1,3); console.log(`Original array: ${mArrayCities}`); console.log(`Copied array: ${mArrCitiesClone}`);
Original array: Barcelona,Paris,Roma,Budapest
Copied array: Paris,Roma
A negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence.
var mArrayCities = ['Barcelona','Paris','Roma','Budapest']; var mArrCitiesClone = mArrayCities.slice(-2); console.log(`Original array: ${mArrayCities}`); console.log(`Copie array: ${mArrCitiesClone}`);
Original array: Barcelona,Paris,Roma,Budapest
Copie array: Roma,Budapest
A negative index can be used, indicating an offset from the end of the sequence. slice(2,-1) extracts the third element through the second-to-last element in the sequence.
var mArrayCities = ['Barcelona','Paris','Roma','Budapest']; var mArrCitiesClone = mArrayCities.slice(1,-1); console.log(`Original array: ${mArrayCities}`); console.log(`Copie array: ${mArrCitiesClone}`);
Original array: Barcelona,Paris,Roma,Budapest
Copie array: Paris,Roma
7.18 Array.some()
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
The some() method does not change the original array.
some() METHOD | |
---|---|
Syntax | mArrayName.some(function(currentValue, index, array),thisArg) |
Parameters |
function: to test for each element. currentValue: the current element being processed in the array. index: the index of the current element being processed in the array. array: the array some() was called upon. thisArg: a value to use as this when executing function. |
Return | A Boolean. Returns true if any of the elements in the array pass the test, otherwise it returns false. |
function __checkEven(pIntNumber){ return pIntNumber % 2 === 0; } var mArrNumbers = [1, 2, 3, 5, 7]; console.log(`An element is even : ${mArrNumbers.some(__checkEven)}`);
An element is even : true
7.19 Array.sort()
The sort() method sorts the elements of an array in place and returns the sorted array.
The default sort order is ascending.
sort() METHOD | |
---|---|
Operador Xsql | <array.sort name='array_name' /> |
Syntax | mArrayName.sort(compareFunction) |
Parameters | compareFunction: Optional. A function that defines an alternative sort order. |
Return | The sorted array. |
<xsql-script> <body> <array name='m_cities'> <string>Paris</string> <string>Roma</string> <string>Barcelona</string> </array> <println>Array cities before sorted: <m_cities /></println> <array.sort name='m_cities' /> <println>Array cities after sorted: <m_cities /></println> </body> </xsql-script>
Array cities before sorted: [Paris,Roma,Barcelona]
Array cities after sorted: [Barcelona,Paris,Roma]
var mArrayCities = ['Paris','Roma','Barcelona']; console.log(`Array cities before sorted: ${mArrayCities}`); mArrayCities.sort(); console.log(`Array cities after sorted: ${mArrayCities}`);
Array cities before sorted: Paris,Roma,Barcelona
Array cities after sorted: Barcelona,Roma,Paris
Compare Function
The function should return a negative, zero, or positive value, depending on the arguments, like:
function(a, b){return a-b}
When the method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.
Example:
When comparing 40 and 100, the sort() method calls the compare function(40,100).
The function calculates 40-100, and returns -60 (a negative value).
The sort function will sort 40 as a value lower than 100.
Sort numbers in an array in descending order:
var mArrNumbers = [40, 100, 1, 5, 25, 10]; console.log(`Array numbers before sorted: ${mArrNumbers}`); mArrNumbers.sort(function(a, b){return b-a}); console.log(`Array numbers after sorted: ${mArrNumbers}`);
Array numbers before sorted: 40,100,1,5,25,10
Array numbers after sorted: 100,40,25,10,5,1
7.20 Array.splice()
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
The splice() method changes the original array.
splice() METHOD | |
---|---|
Operador Xsql | <array.remove name='array_name' /> |
Syntax | mArrayName.splice(start, deleteCount, element1, ....., elementX) |
Parameters |
index: Required. The index at which to start changing the array. howmany: Optional. An integer indicating the number of elements in the array to remove from start. compareFunction: Optional. The elements to add to the array, beginning from start. |
Return | The sorted array. |
<xsql-script> <body> <array name='m_cities'> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> <println>Array cities before remove element: <m_cities /></println> <array.remove name='m_cities'> <string>1</string> </array.remove> <println>Array cities after remove element: <m_cities /></println> </body> </xsql-script>
Array cities before remove element: [Barcelona,Paris,Roma]
Array cities after remove element: [Barcelona,Roma]
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Array cities before remove element: ${mArrayCities}`); mArrayCities.splice(1, 1); console.log(`Array cities after remove element: ${mArrayCities}`);
Array cities before remove element: Barcelona,Paris,Roma
Array cities after remove element: Barcelona,Roma
Remove items from an index position
var mArrayCities = ['Barcelona','Paris','Roma','Budapest']; console.log(`Array cities before remove element: ${mArrayCities}`); mArrayCities.splice(1, 2); console.log(`Array cities after remove element: ${mArrayCities}`);
Array cities before remove element: Barcelona,Paris,Roma,Budapest
Array cities after remove element: Barcelona,Budapest
7.21 Array.toString()
The toString() method returns a string representing the specified array and its elements.
toString() METHOD | |
---|---|
Syntax | mArrayName.toString() |
Parameters | No parameters. |
Return | A string representing the elements of the array |
var mArrFruits = ["Banana", "Orange", "Apple", "Mango"]; var mStrFruits = mArrFruits.toString(); console.log(`Array become to string: ${mStrFruits}`);
Array become to string: Banana,Orange,Apple,Mango
7.22 Array.unshift
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
unshift() METHOD | |
---|---|
Syntax | mArrayName.unshift(element[1], element[2], ... element[n]) |
Parameters | The element(s) to add to the begining of the array. |
Return | A Number, representing the new length of the array. |
<xsql-script> <body> <set name='m_cities'> <array> <string>Barcelona</string> <string>Paris</string> <string>Roma</string> </array> </set> <println>Array cities before add element: <m_cities /></println> <!--Save the elements of the original in a temporal array--> <set name='m_cities_temp'> <array.clone name='m_cities' /> </set> <!--Clean the original array--> <array.clear name='m_cities' /> <!--Add the element at the first position--> <array.add name='m_cities'> <string>Budapest</string> </array.add> <!--Loop through the m_cities_temp array to add at the original array--> <iterator name='m_city_temp'> <in> <m_cities_temp/> </in> <do> <array.add name='m_cities'> <m_city_temp /> </array.add> </do> </iterator> <!--Clean the temporal array--> <array.clear name='m_cities_temp' /> <println>Array cities after add element: <m_cities /></println> </body> </xsql-script>
Array cities before add element: [Barcelona,Paris,Roma]
Array cities after add element: [Budapest,Barcelona,Paris,Roma]
var mArrayCities = ['Barcelona','Paris','Roma']; console.log(`Array cities before add element: ${mArrayCities}`); mArrayCities.unshift('Budapest'); console.log(`Array cities after add element: ${mArrayCities}`);
Array cities before add element: Barcelona,Paris,Roma
Array cities after add element: Budapest,Barcelona,Paris,Roma