Arrays
An array is a collection of items or values. Array Indexes starts with zero.
Syntax
let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);
Example
let mobiles=["iPhone", "Samsung", "Pixel"];
// accessing an array
console.log(mobiles[0]);
//changing an array element
mobiles[3] = "Nokia";
Array Iteration Methods
ES6 has brought many improvements to Javascript language and made developer's job easy with the introduction of many new functions. Below are some of the most useful array methods in Javascript.
1. forEach()
forEach method is used to run a function on every element present in an array. This method can only be used on Arrays, Maps and Sets.
You can either use callback functions or Arrow functions in forEach method.
Remember that you can not break, return or continue in forEach method like you do in traditional for loop. If you want to do so, then use exceptions in the call back function.
Syntax
arrayname.forEach(function(err, doc) {
//code
});
Example
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = [];
numbers.forEach(function(ele) {
if(ele % 2 == 0)
squaresOfEvenNumbers.push(ele*ele)
});
console.log(squaresOfEvenNumbers);
2. map()
This method is used to create a new array by running a function on every element of an array.
Syntax
arrayName.map(function(err, doc)) {
//code
});
// Arrow function can also be used in place of callback function
3. filter()
This method is used to create a new array by running a function on every element of an array based on a criteria .
Syntax
arrayName.filter(function(err, doc)) {
//code
});
// Arrow function can also be used in place of callback function
4. reduce()
This method is used to produce a single value by running a function on every element of an array.
Syntax
arrayName.reduce(function(err, doc)) {
//code
});
// Arrow function can also be used in place of callback function
Example
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers
.filter(ele => ele % 2 == 0)
.map(ele => ele ** 2)
.reduce((acc, ele) => acc + ele);
console.log("sum of squares of even numbers:");
console.log(squaresOfEvenNumbers);
Run here
5. find()
This method returns the first element of the array which passes the criteria.
Syntax
let find_value = arrayName.find(function(err, doc)) {
//code
});
// Arrow function can also be used in place of callback function
Example
let emp = [
{ name: "foo", age: 16 },
{ name: "bar", age: 13 },
{ name: "Mark", age: 25 },
{ name: "John", age: 35 }
];
firstMatch = emp.find(emp => emp.age > 18)
console.log(firstMatch);
Run here
6. indexOf()
This method returns the index of a particular element.
Syntax
let index = arrayName.indexOf(element);
Example
let arr = ['foo', 'bar', 'mark'];
if(arr.indexOf('mark') > -1) {
console.log('value is present in the array');
} else {
console.log('value is not present in the array');
}
Run here
Note: lastIndexOf() is similar to indexOf() but returns the index of last occurence of the element.
7. from()
This method creates a new instance of the array from an array-like or iterable object.
Syntax
let newArray= Array.from(arrayLike[, mapFn[, thisArg]]);
Example
let numbers = [1, 2, 3, 4];
const squaresOfNumbers= Array.from(numbers, value => value * value);
console.log(squaresOfNumbers);
Run here
8. every()
This method returns true if all the elements present in the array passes the criteria else false.
Syntax
Array.every(callback(element[, index[, array]])[, thisArg])
Example
let emp = [
{ name: "foo", age: 16 },
{ name: "bar", age: 13 },
{ name: "Mark", age: 25 },
{ name: "John", age: 35 }
];
adultCheck = emp.every(emp => emp.age > 18)
console.log(adultCheck);
Run here
9. some()
This method returns true if atleast one element present in the array passes the criteria else false.
Syntax
Array.some(callback(element[, index[, array]])[, thisArg])
Example
let emp = [
{ name: "foo", age: 16 },
{ name: "bar", age: 13 },
{ name: "Mark", age: 25 },
{ name: "John", age: 35 }
];
adultPresent = emp.some(emp => emp.age > 18)
console.log(adultPresent);
Run here
10. includes()
This method returns true if an element is present in the array else false. You can also specify an optional second parameter like shown below which specifies the position to start the search.
Syntax
arrayName.includes(value-to-be-checked[, starting-search-index])
Example
let arr = ['foo', 'bar', 'mark'];
console.log(arr.includes('bar'));
console.log(arr.includes('bar',1)); // optional second parameter specifies the position to start the search.