Earid

JavaScript Array Object

20/04/2020

by Earid

Array Properties

Property Description
length Sets or returns the number of elements in an array.
prototype Allows you to add new properties and methods to an Array object.

Array Methods

Method Description
concat() Merge two or more arrays, and returns a new array.
copyWithin() Copies part of an array to another location in the same array and returns it.
entries() Returns a key/value pair Array Iteration Object.
every() Checks if every element in an array pass a test in a testing function.
const arr = [1, 2, 3, 4, 5, 6]; // all elements are greater than 4 const greaterFour = arr.every(num => num > 4); console.log(greaterFour); // output: false // all elements are less than 10 const lessTen = arr.every(num => num < 10); console.log(lessTen); // output: true
fill() Fill the elements in an array with a static value.
filter() This method create new array with only elements passed condition inside the provided function.
const arr = [1, 2, 3, 4, 5, 6]; // item(s) greater than 3 const filtered = arr.filter(num => num > 3); console.log(filtered); // output: [4, 5, 6] console.log(arr); // output: [1, 2, 3, 4, 5, 6]
find() Returns the value of the first element in an array that pass the test in a testing function.
findIndex() Returns the index of the first element in an array that pass the test in a testing function.
forEach() Calls a function once for each array element.
const arr = [1, 2, 3, 4, 5, 6]; arr.forEach(item => { console.log(item); // output: 1 2 3 4 5 6 });
from() Creates an array from an object.
includes() This method check if array includes the item passed in the method.
const arr = [1, 2, 3, 4, 5, 6]; arr.includes(2); // output: true arr.includes(7); // output: false
indexOf() Search the array for an element and returns its first index.
isArray() Determines whether the passed value is an array.
join() Joins all elements of an array into a string.
keys() Returns a Array Iteration Object, containing the keys of the original array.
lastIndexOf() Search the array for an element, starting at the end, and returns its last index.
map() Creates a new array with the results of calling a function for each array element.
const arr = [1, 2, 3, 4, 5, 6]; // add one to every element const oneAdded = arr.map(num => num + 1); console.log(oneAdded); // output [2, 3, 4, 5, 6, 7] console.log(arr); // output: [1, 2, 3, 4, 5, 6]
pop() Removes the last element from an array, and returns that element.
push() Adds one or more elements to the end of an array, and returns the array’s new length.
reduce() Reduce the values of an array to a single value (from left-to-right).
const arr = [1, 2, 3, 4, 5, 6]; const sum = arr.reduce((total, value) => total + value, 0); console.log(sum); // 21
reduceRight() Reduce the values of an array to a single value (from right-to-left).
reverse() Reverses the order of the elements in an array.
shift() Removes the first element from an array, and returns that element.
slice() Selects a part of an array, and returns the new array.
some() Checks if any of the elements in an array passes the test in a testing function.
const arr = [1, 2, 3, 4, 5, 6]; // at least one element is greater than 4? const largeNum = arr.some(num => num > 4); console.log(largeNum); // output: true // at least one element is less than or equal to 0? const smallNum = arr.some(num => num <= 0); console.log(smallNum); // output: false
sort() Sorts the elements of an array
const arr = [1, 2, 3, 4, 5, 6]; const alpha = ['e', 'a', 'c', 'u', 'y']; // sort in descending order descOrder = arr.sort((a, b) => a > b ? -1 : 1); console.log(descOrder); // output: [6, 5, 4, 3, 2, 1] // sort in ascending order ascOrder = alpha.sort((a, b) => a > b ? 1 : -1); console.log(ascOrder); // output: ['a', 'c', 'e', 'u', 'y']
splice() Adds/Removes elements from an array.
toString() Converts an array to a string, and returns the result.
unshift() Adds new elements to the beginning of an array, and returns the array’s new length.
values() Returns a Array Iteration Object, containing the values of the original array.
Facebook
X
LinkedIn
Email
Pinterest
WhatsApp

JavaScript Array Object