Javascript Array
What is an Array?¶
- Stores multiple elements in a single variable
- No restrictions on data types
- Used frequently in solving problems
How to create an Array?¶
-
Array with no elements
var arr = [];
-
Array with a few elements
var fruits = ["apple", "banana"];
var numbers = [1.2, 56, -10]
var mixed = ["apple", 1, false, null]
Nested Arrays¶
- Nested arrays are arrays which contain arrays as elements
var data = [ ["apple", "banana"], ["fish", "bun", 100], ];
Accessing & Modifying an Array with indexes¶
- Arrays stored in a sequencial manner with an index.
-
Index always starts with
0
-
Let's see an example to access array elements with indexes
var fruits = ["apple", "banana", "grapes", "mango"]; var apple = fruits[0] var banana = fruits[1] var grapes = fruits[2] var mango = fruits[3] console.log(apple); // output: apple
-
Let's see an example to update array elemets with indexes
var fruits = ["apple", "banana", "grapes", "mango"]; console.log("before update:", fruits) // output: before update: ["apple", "banana", "grapes", "mango"] fruits[0] = "orange" console.log("after update:", fruits) // output: before update: ["orange", "banana", "grapes", "mango"]
If you execute the above code you will find that we have updated the first element from value apple
to orange
Accessing multi dimentional arrays with indexes¶
- Multidimentional arrays are nothing but nested arrays
- We don't use these multi dimentional arrays frequently
var food_items = [
["apple", "banana", "orange"],
["bun", "pizza", "cake"]
]
var apple = food_items[0][0];
var banana = food_items[0][1];
var orange = food_items[0][2];
var bun = food_items[1][0];
var pizza = food_items[1][1];
var cake = food_items[1][2];
console.log(cake)
// output: cake
Add element to an array at the end with push()
method¶
var fruits = ["apple"]
fruits.push("banana")
console.log(fruits)
// output: ["apple", "banana"]
banana
at the end of the array fruits
Add element to an array at the beginning with unshift()
method¶
var fruits = ["apple"]
fruits.unshift("banana")
console.log(fruits)
// output: ["banana", "apple"]
banana
at the beginning of the array fruits
Remove last element from an array with pop()
method¶
var fruits = ["apple", "banana"]
var banana = fruits.pop()
console.log(fruits)
// output: ["apple"]
console.log(banana)
// output: banana
fruits
and returns the element. - Returned element stored in the variable banana Remove first element from an array with shift()
method¶
var fruits = ["apple", "banana"]
var apple = fruits.shift()
console.log(fruits)
// output: ["banana"]
console.log(apple)
// output: apple
banana
from beginning of the array fruits
- Returened element stored in the variable apple
Loop over an Array¶
let fruits = ['Apple', 'Banana']
fruits.forEach(function(item, index) {
console.log(item, index)
})
/* output:
Apple 0
Banana 1
*/
Remove items from an index position using splice(index, numberOfElements)
¶
let vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
console.log(vegetables)
// ["Cabbage", "Turnip", "Radish", "Carrot"]
let pos = 1
let n = 2
let removedItems = vegetables.splice(pos, n)
// this is how to remove items, n defines the number of items to be removed,
// starting at the index position specified by pos and progressing toward the end of array.
console.log(vegetables)
// ["Cabbage", "Carrot"] (the original array is changed)
console.log(removedItems)
// ["Turnip", "Radish"]
Copy an Array¶
let vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
let shallowCopy = vegetables.slice()
console.log(shallowCopy)
// ['Cabbage', 'Turnip', 'Radish', 'Carrot']
Finding length of javascript array¶
let vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
let count = vegetables.length
console.log(count)
// 4
Find the index of an item in the Array¶
let vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
let pos = vegetables.indexOf('Turnip')
console.log(pos)
// 1
Reverse an array javascript¶
let vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
let reverse_order = vegetables.reverse();
console.log(reverse_order)
// ["Cabbage", "Turnip", "Radish", "Carrot"]