Arrays

What is array?
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Syntax
Array declaration syntax is very simple. The syntax is the same as for a normal variable declaration except the variable name should be followed by subscripts to specify the size of each dimension of the array. The general form for an array declaration would be:
VariableType varName[dim1, dim2, ..., dimN];
where VariableType is a Modelica type like Real or Integer, varName is the name of the variable.
Array Index: The location of an element in an array has an index, which identifies the element. Array index starts from 0.
Array element: Items stored in an array is called an element. The elements can be accessed via its index.
Array Length: The length of an array is defined based on the number of elements an array can store. In the above example, array length is 6 which means that it can store 6 elements.
Declaration of Array
let food = ['Rice', 1, 2, 'hashnode', true]
Array.push()
The JavaScript array.push() method is used to add values to the end of an array. With this method, you can increase an array length as new values are added. There is no limit to new values you can add therefore it solely depends on you to know the number of new values to be inserted. Let me show you an example
let food = ['Rice', 1, 2, 'hashnode', true]
food.push('hashnodeSeries', 10, 20)
//returns ['Rice', 1, 2, 'hashnode', true,'hashnodeSeries', 10, 20]
Array.unshift()
This JavaScript array method can be said to be similar to the array.push() method which means it is used to add values. But with the unshift method, it adds values to the beginning of the array. The length of the array will be increased and there is no limit to the number of values we can pass in. Let's have a look at how it works
let arr = ['hashnode', 1, 2,3, false]
arr.unshift('welcome', 'to', 'my', 'blog')
console.log(arr) // returns ['welcome','to','my','blog', 'hashnode',1,2,3,false]
Array.shift()
This JavaScript array method works like the array.pop() method which means it is used to remove values from an array. It removes an array element from the beginning of the array. It does not pass any argument and decreases the length of the array when called. Let's take a look at the syntax
let arr = ['hashnode', 1, 2,3, false]
arr.shift()
console.log(arr) // returns [1,2,3,false]
Array.pop()
The JavaScript array.pop() method as the name sounds is used to pop out something. This method removes the last element in an array. It returns the removed element and decreases the length of the array. It does not pass any argument so if the array is empty, it returns undefined. It is the opposite of the array.push() method. Let's have a look at the syntax
let food = ['Rice', 1, 2, 'hashnode', true]
food.pop()
//returns ['Rice', 1, 2, 'hashnode']
Array.splice()
The array.splice() method returns the removed item(s) in an array. This method changes the original array. It specifically takes three(3) parameters but the third parameter is for adding new items to the array so it can be more than one. Let me explain:
Syntax
array.splice(startindex, numberOfElementsToBeRemoved, newItems)
- The first parameter is the array index which specifies the position to start removing/adding the item(s).
- The second parameter is the number of items to remove from the array (remember not index). If set to zero(0), no item will be removed and if not passed, all item(s) from the provided index will be removed.
- The third parameter is for the new item(s) to be added into the array. This is completely optional in case you don't want to add anything. You can add anything like string, number, or boolean values. Let's take a look at code examples.
let num = [1,2,3,4,5,6,7,8,9,10]
console.log(num.splice(3,4)) //returns the removed items as a new array [4,5,6,7]
console.log(num) //returns the altered original array [1,2,3,8,9,10]
let num = [1,2,3,4,5,6,7,8,9,10]
console.log(num.splice(3,4, 'new', 'item')) //returns the removed items as a new array [4,5,6,7]
console.log(num) //returns the array with the added items [1,2,3,'new','item',8,9,10]
Array.slice()
This method returns the selected element(s) in an array, as a new array object. This method doesn't change the original array (the original array is left untouched) and it takes just two parameters. It deals with only the array indexes which is one major difference it has from the splice() method.
Syntax
array.slice(start, until)
- The first parameter is the index at which to begin the extraction.
- The second parameter is the index at which to end the extraction. If this parameter is omitted, all elements from the start of the array to end will be selected. The element at this position won't be selected Let's see an example.
let num = [1,2,3,4,5,6,7,8,9,10]
console.log(num.slice(3)) //doesn't have the last parameter so it retunns all the selected item [4,5,6,7,8,9,10]
let num = [1,2,3,4,5,6,7,8,9,10]
console.log(num.slice(2,5)) //starts from index 3 until index 5 but won't select the element at index 5. returns [3,4,5]
Both array methods (i.e splice and slice) work with array indexes and just to remind you that array indexes start from 0
