JS 数组方法!🐱🏍
1)Array.prototype.map()
2)Array.prototype.filter()
3)Array.prototype.sort()
3)Array.prototype.reduce()
就是这样
额外表情包
编码愉快🚀
什么是 JS 数组?
JavaScript Array 类是一个全局对象,用于构造数组;数组是高级的、类似列表的对象。
数组提供了很多方法,让事情变得简单。
我们将讨论 4 种数组方法:
1.地图
2.过滤
3.排序
4.减少
1)Array.prototype.map()
因此,使用 map() 方法的基本需求是修改给定的数据,map() 方法创建一个新数组,该数组填充了对调用数组中每个元素调用提供的函数的结果。它返回与数组传递的数据量相同但经过修改的数据形式
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()
因此,使用 filter() 方法的基本需求是过滤给定的数据。filter() 方法会创建一个新数组,其中包含所有通过指定函数测试的元素。
它会返回过滤后的数组,但其中可能不包含你传入的每个元素。
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()
因此,使用 sort() 方法的基本需求是对给定的数据进行排序,sort() 方法会将数组元素按位置排序并返回排序后的数组。默认排序顺序为升序。它将返回与传入数据相同的数据量!
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()
因此,使用 reduce() 方法的基本需求是对给定的数据进行排序,reduce() 方法对数组的每个元素执行一个 reducer 函数(即您提供的),从而产生单个输出值,它返回单个值。
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);
就是这样
本博客的灵感来自 Wes Bos JavaScript30 课程