Add a new element to the end of the array.
const names = ['Luis','John','Jose'];names.push("Aaron");console.log(names); // (4) ["Luis", "John", "Jose", "Aaron"]
Delete the last element of the array
const names = ['Luis','John','Jose','Aaron'];console.log(names.pop()); //Aaronconsole.log(names); // (3) ["Luis", "John", "Jose"]
Delete the first element of the array
const names = ['Luis','John','Jose','Aaron'];console.log(names.shift()); // Luisconsole.log(names); // (3) ["John", "Jose", "Aaron"]
Add one or more elements in the beginning of the array
const names = ['Luis','John','Jose'];console.log(names.unshift("Aaron")); // 4console.log(names); // (4) ["Aaron", "Luis", "John", "Jose"]
Remove, add or replace a new element indicate by index.
const names = ['Luis','John','Jose','Aaron'];console.log(names.splice(0,0,"Fernando")); // Add Michelleconsole.log(names.splice(0,1,"Michelle")); // replace Fernando to Michelleconsole.log(names.splice(0,1)); // remove Michelleconsole.log(names);
Modify the array, ordered by a compare Function, or if this compare function is not provided the default order is by the position of the Unicode values in the array.
const names = ['Luis','Jose','John','Aaron'];console.log(names.sort()); // (4) ["Aaron", "John", "Jose", "Luis"]/*complex sorting*/const users = [{name:'Luis', age:25},{name:'Jose', age:20},{name:'Aaron', age:40}];const compareFuc = (item1,item2) => {return item1.age - item2.age;};console.log(users.sort(compareFuc));/**[{name: "Jose", age: 20}, {name: "Luis", age: 25}, {name: "Aaron", age:40}]*/
Create a new array with the union of two or more arrays.
const names1 = ["Luis","Jose"];const names2 = ["John","Aaron"];const newArray = names1.concat(names2,["Michelle"]);console.log(newArray); // (5) ["Luis", "Jose", "John", "Aaron", "Michelle"]
Return a copy of a sub array between two index, start and end. Important Note: if you modify the original array, the value also will be modify in the copy array.
const users = [{name:'Luis', age:15},{name:'Jose', age:18},{name:'Aaron', age:40}];const adults = users.slice(1, users.length);console.log(adults); // (2) [{name: "Jose", age: 18}, {name: "Aaron", age: 40}]
Return the first index of the element that exists in the array, and if not exists return-1.
const names = ['Luis','Jose','John','Aaron'];console.log(names.indexOf("John")); // 2console.log(names.indexOf("Michelle")); // -1
Just execute a function for each element in the array.
const names = ['Luis','Jose','John','Aaron'];names.forEach(item => {console.log(item);});
Create a new array with the result of the callback function (this function is executed for each item same as forEach)
const users = [{name:'Luis', age:15},{name:'Jose', age:18},{name:'Aaron', age:40}];const userDescriptions = users.map(item => {return `Hello my name is ${item.name} and I have ${item.age} years old.`});console.log(userDescriptions);/*["Hello my name is Luis and I have 15 years old.","Hello my name is Jose and I have 18 years old.","Hello my name is Aaron and I have 40 years old."] */
Create a new array with the elements that apply the given filter condition as true.
const users = [{name:'Luis', admin:true},{name:'Jose', admin:true},{name:'Aaron'}];const adminUsers = users.filter(item => item.admin);console.log(adminUsers); // [{name: "Luis", admin: true},{name: "Jose", admin: true}]
Return a single value after applying the reduction function for each element.
const users = [{name:'Luis', age:15},{name:'Jose', age:18},{name:'Aaron', age:40}];const reducer= (accumulator, item)=> accumulator + item.age;const totalAge = users.reduce(reducer,0);const ageAverage = totalAge / users.length;console.log(`Total ${totalAge}, Average ${ageAverage}`); // Total 73, Average 24.333333333333332
Return a boolean value as true if found one or more item that apply the given condition, and return false if not (also if the array is empty).
const users = [{name:'Luis', admin:true},{name:'Jose'},{name:'Aaron'}];const adminExists = users.some(item => item.admin);console.log(adminExists); // true
This function Return a boolean value as true if all the items apply the given condition, and false if not.
const users = [{name:'Luis', active:true},{name:'Jose', active:true},{name:'Aaron', active:false}];const isAllUsersActive = users.every(item => item.active);console.log(isAllUsersActive); // false
Quick Links
Legal Stuff
Social Media