Sitemap
Johnhoro

Johnhoro

Mostly use JavaScript Array methods

1 min readFeb 10, 2022

--

1. map()

It returns a new array containing the results of calling a function on every element in this array.

var array1 = [1, 2, 9, 16];// pass a function to map
var map1 = array1.map(x => x * 3);
console.log(map1);
// expected output: Array [3, 6, 27, 48]

2. forEach()

It calls a function for each element in the array.

var array1 = ['x', 'y', 'z'];array1.forEach(element => console.log(element));// expected output: "x"
// expected output: "y"
// expected output: "z"

Get Johnhoro’s stories in your inbox

Join Medium for free to get updates from this writer.

3. filter()

It returns a new array containing all elements of the calling array for which the provided filtering function returns true.

var array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];var result = array.filter((elm)=> elm >5);console.log(result);
// expected output: Array [6, 7, 8, 9, 10, 11, 12, 13]

4. includes()

It determines whether the array contains a value, returning true or false .

var pets = ['cat', 'dog', 'bat'];console.log(pets.includes('cat'));
// expected output: true

5. splice()

It adds and/or removes elements from an array.

var months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

--

--