WHAT IS A JS ARRAY ?
The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.
Arrays provide a lot of methods. To make things easier.
We will be talking about 4 Array Methods :
1.map
2.filter
3.sort
4.reduce
1) Array.prototype.map()
So the basic need for using the map() method is to modify a given data, the map() method creates a new array populated with the results of calling a provided function on every element in the calling array. it return the same amount of data passed by the array but in a modified form
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }
const fullName = inventors.map(
inventor => `${inventor.first} ${inventor.last}`
);
console.log(fullName); // it returns the full name of the inventors using the map method
2) Array.prototype.filter()
So the basic need for using the filter() method is to filter out a given data, the filter() method creates a new array with all elements that pass the test implemented by the provided function.
Its returns the filtered arrays which might not include every element you have passed init.
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }
const filter = inventors.filter(
inventor => inventor.year >= 1500 && inventor.year <= 1599
);
console.table(filter); // filter helps us here to filter out the list of inventors year dates
3) Array.prototype.sort()
So the basic need for using the sort() method is to sort out a given data, the sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending. It will return the same amount of data that has been passed !
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }
const sorted = inventors.sort((a, b) => (a.passed > b.passed ? 1 : -1));
console.table(sorted); // this method helps with the sorting of the results/arrays
3) Array.prototype.reduce()
So the basic need for using the reduce() method is to sort out a given data, the reduce() method executes a reducer function i.e (that you provide) on each element of the array, resulting in a single output value, It returns a single value.
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }
const total = inventors.reduce((total, inventor) => {
return total + (inventor.passed - inventor.year);
}, 0); // this method helps us to calculate the total number of years that were lived by the inventors using the reduce method
console.log(total);
Some more JS Array Methods are:-
That's IT
This Blog was inspired was Wes Bos JavaScript30 course
Top comments (7)
No mention of
forEach
?Also, arrays in Lua do in fact start at index 1 by convention
Actually I have kept forEach for another blog where I will be discussing about in much further detail, and the Lua things is fresh knowledge to me, thanks π€
Well done! Thank you for sharing your knowledge with us ππΌ
I am just going to to leave this MDN link here for those that are interested in learning more about arrays
developer.mozilla.org/en-US/docs/W...
ππ€
Very clear, thank you)
Your Welcome π€