28 个 Javascript 数组方法汇总整理

const list = [😫, 😫, 😫, 😫];list.map((⚪️) => 😀); // [😀, 😀, 😀, 😀]// Codeconst list = [1, 2, 3, 4];list.map((el) => el * 2); // [2, 4, 6, 8]
const list = [😀, 😫, 😀, 😫];list.filter((⚪️) => ⚪️ === 😀); // [😀, 😀]// Codeconst list = [1, 2, 3, 4];list.filter((el) => el % 2 === 0); // [2, 4]
const list = [😀, 😫, 😀, 😫, 🤪];list.reduce((⬜️, ⚪️) => ⬜️ + ⚪️); // 😀 + 😫 + 😀 + 😫 + 🤪// ORconst list = [1, 2, 3, 4, 5];list.reduce((total, item) => total + item, 0); // 15
const list = [😀, 😫, 😀, 😫, 🤪];list.reduceRight((⬜️, ⚪️) => ⬜️ + ⚪️); // 🤪 + 😫 + 😀 + 😫 + 😀// Codeconst list = [1, 2, 3, 4, 5];list.reduceRight((total, item) => total + item, 0); // 15
const list = [😀, 😫, 😀, 😫, 🤪];list.fill(😀); // [😀, 😀, 😀, 😀, 😀]// Codeconst list = [1, 2, 3, 4, 5];list.fill(0); // [0, 0, 0, 0, 0]
const list = [😀, 😫, 😀, 😫, 🤪];list.find((⚪️) => ⚪️ === 😀); // 😀list.find((⚪️) => ⚪️ === 😝); // undefined// Codeconst list = [1, 2, 3, 4, 5];list.find((el) => el === 3); // 3list.find((el) => el === 6); // undefined
const list = [😀, 😫, 😀, 😫, 🤪];list.indexOf(😀); // 0list.indexOf(😡); // -1// Codeconst list = [1, 2, 3, 4, 5];list.indexOf(3); // 2list.indexOf(6); // -1
const list = [😀, 😫, 😀, 😫, 🤪];list.lastIndexOf(😀); // 3list.lastIndexOf(😀, 1); // 0// Codeconst list = [1, 2, 3, 4, 5];list.lastIndexOf(3); // 2list.lastIndexOf(3, 1); // -1
const list = [😀, 😫, 😀, 😫, 🤪];list.findIndex((⚪️) => ⚪️ === 😀); // 0// You might be thinking how it's different from `indexOf` 🤔const array = [5, 12, 8, 130, 44];array.findIndex((element) => element > 13); // 3// ORconst array = [{id: 😀}, {id: 😫}, {id: 🤪}];array.findIndex((element) => element.id === 🤪); // 2
const list = [😀, 😫, 😀, 😫, 🤪];list.includes(😀); // true// Codeconst list = [1, 2, 3, 4, 5];list.includes(3); // truelist.includes(6); // false
const list = [😀, 😫, 😀, 😫, 🤪];list.pop(); // 🤪list; // [😀, 😫, 😀, 😫]// Codeconst list = [1, 2, 3, 4, 5];list.pop(); // 5list; // [1, 2, 3, 4]
const list = [😀, 😫, 😀, 😫, 🤪];list.push(😡); // 5list; // [😀, 😫, 😀, 😫, 🤪, 😡]// Codeconst list = [1, 2, 3, 4, 5];list.push(6); // 6list; // [1, 2, 3, 4, 5, 6]
const list = [😀, 😫, 😀, 😫, 🤪];list.shift(); // 😀list; // [😫, 😀, 😫, 🤪]// Codeconst list = [1, 2, 3, 4, 5];list.shift(); // 1list; // [2, 3, 4, 5]
const list = [😀, 😫, 😀, 😫, 🤪];list.unshift(😡); // 6list; // [😡, 😀, 😫, 😀, 😫, 🤪]// Codeconst list = [1, 2, 3, 4, 5];list.unshift(0); // 6list; // [0, 1, 2, 3, 4, 5]
const list = [😀, 😫, 😀, 😫, 🤪];list.splice(1, 2); // [😀, 😫]list; // [😀, 😫, 🤪]// Codeconst list = [1, 2, 3, 4, 5];list.splice(1, 2); // [2, 3]list; // [1, 4, 5]
const list = [😀, 😫, 😀, 😫, 🤪];list.slice(1, 3); // [😫, 😀]list; // [😀, 😫, 😀, 😫, 🤪]// Codeconst list = [1, 2, 3, 4, 5];list.slice(1, 3); // [2, 3]list; // [1, 2, 3, 4, 5]
const list = [😀, 😫, 😀, 😫, 🤪];list.join('〰️'); // "😀〰️😫〰️😀〰️😫〰️🤪"// Codeconst list = [1, 2, 3, 4, 5];list.join(', '); // "1, 2, 3, 4, 5"
const list = [😀, 😫, 😀, 😫, 🤪];list.reverse(); // [🤪, 😫, 😀, 😫, 😀]list; // [🤪, 😫, 😀, 😫, 😀]// Codeconst list = [1, 2, 3, 4, 5];list.reverse(); // [5, 4, 3, 2, 1]list; // [5, 4, 3, 2, 1]
const list = [😀, 😫, 😀, 😫, 🤪];list.sort(); // [😀, 😀, 😫, 😫, 🤪]// This make more sense 🤔const array = ['D', 'B', 'A', 'C'];array.sort(); // 😀 ['A', 'B', 'C', 'D']// ORconst array = [4, 1, 3, 2, 10];array.sort(); // 😧 [1, 10, 2, 3, 4]array.sort((a, b) => a - b); // 😀 [1, 2, 3, 4, 10]
const list = [😀, 😫, 😀, 😫, 🤪];list.some((⚪️) => ⚪️ === 😀); // truelist.some((⚪️) => ⚪️ === 😡); // false// Codeconst list = [1, 2, 3, 4, 5];list.some((el) => el === 3); // truelist.some((el) => el === 6); // false
const list = [😀, 😫, 😀, 😫, 🤪];list.every((⚪️) => ⚪️ === 😀); // falseconst list = [😀, 😀, 😀, 😀, 😀];list.every((⚪️) => ⚪️ === 😀); // true// Codeconst list = [1, 2, 3, 4, 5];list.every((el) => el === 3); // falseconst list = [2, 4, 6, 8, 10];list.every((el) => el%2 === 0); // true
const list = 😀😫😀😫🤪;Array.from(list); // [😀, 😫, 😀, 😫, 🤪]const set = new Set(['😀', '😫', '😀', '😫', '🤪']);Array.from(set); // [😀, 😫, 🤪]const range = (n) => Array.from({ length: n }, (_, i) => i + 1);console.log(range(10)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const list = Array.of(😀, 😫, 😀, 😫, 🤪);list; // [😀, 😫, 😀, 😫, 🤪]// Codeconst list = Array.of(1, 2, 3, 4, 5);list; // [1, 2, 3, 4, 5]
Array.isArray([😀, 😫, 😀, 😫, 🤪]); // trueArray.isArray(🤪); // false// CodeArray.isArray([1, 2, 3, 4, 5]); // trueArray.isArray(5); // false
const list = [😀, 😫, 😀, 😫, 🤪];list.at(1); // 😫// Return from last 🤔list.at(-1); // 🤪list.at(-2); // 😫// Codeconst list = [1, 2, 3, 4, 5];list.at(1); // 2list.at(-1); // 5list.at(-2); // 4
const list = [😀, 😫, 😀, 😫, 🤪];list.copyWithin(1, 3); // [😀, 😀, 🤪, 😫, 🤪]const list = [😀, 😫, 😀, 😫, 🤪];list.copyWithin(0, 3, 4); // [😫, 😫, 😀, 😫, 🤪]// Codeconst list = [1, 2, 3, 4, 5];list.copyWithin(0, 3, 4); // [4, 2, 3, 4, 5]
const list = [😀, 😫, [😀, 😫, 🤪]];list.flat(Infinity); // [😀, 😫, 😀, 😫, 🤪]// Codeconst list = [1, 2, [3, 4, [5, 6]]];list.flat(Infinity); // [1, 2, 3, 4, 5, 6]
const list = [😀, 😫, [😀, 😫, 🤪]];list.flatMap((⚪️) => [⚪️, ⚪️ + ⚪️ ]); // [😀, 😀😀, 😫, 😫😫, 😀, 😀😀, 😫, 😫😫, 🤪, 🤪🤪]// Codeconst list = [1, 2, 3];list.flatMap((el) => [el, el * el]); // [1, 1, 2, 4, 3, 9]
总结
以上就是我今天跟你分享的28个Javascript 数组方法,希望对你有帮助。如果你觉得有用的话,请记得点赞我,关注我,并将其分享给你身边的朋友,也许能够帮助到他。
最后感谢你的阅读。
学习更多技能
请点击下方公众号
![]()

评论
