数组方法

101 阅读6分钟

创建一个示例数组

const array = [1, 2, 3, 4, 5];

  • push() - 在数组末尾添加一个或多个元素

 array.push(6);
  • pop() - 移除并返回数组末尾的元素

 const poppedElement =  array.pop();
  • unshift() - 在数组开头添加一个或多个元素

 array.unshift(0);
  • shift() - 移除并返回数组开头的元素

 const shiftedElement =  array.shift();
  • concat() - 合并多个数组为一个新数组

 const new array =  array.concat([6, 7, 8]);
  • slice() - 截取数组的一部分,返回一个新数组

 const sliced array =  array.slice(1, 3);
  • splice() - 修改数组,可用于插入、替换或删除元素

 array.splice(2, 1, 3.5); // 在索引2插入3.5,删除1个元素
  • indexOf() - 查找元素在数组中首次出现的索引

 const index =  array.indexOf(4);
  • lastIndexOf() - 查找元素在数组中最后一次出现的索引

 const lastIndex =  array.lastIndexOf(3);
  • forEach() - 遍历数组的每个元素执行回调函数

 array.forEach(item => {
 console.log(item);
 });
  • map() - 使用回调函数映射数组的每个元素到新数组

 const mapped array =  array.map(item => item * 2);
  • filter() - 使用回调函数筛选出满足条件的元素到新数组

 const filtered array =  array.filter(item => item > 2);
  • reduce() - 使用回调函数对数组元素进行累积计算

 const sum =  array.reduce((acc, item) => acc + item, 0);
  • find() - 使用回调函数查找满足条件的第一个元素

 const foundElement =  array.find(item => item > 3);
  • every() - 检查数组所有元素是否都满足条件

 const allGreaterThanZero =  array.every(item => item > 0);
  • some() - 检查数组是否存在满足条件的元素

 const hasNegativeNumber =  array.some(item => item < 0);
  • sort() - 对数组元素进行排序

 array.sort();
  • reverse() - 反转数组元素的顺序

 array.reverse();
  • join() - 将数组元素以指定分隔符连接成字符串

 const joinedString =  array.join(', ');
  • includes() - 检查数组是否包含指定元素

 const includesElement =  array.includes(3);
  • is array() - 检查是否为数组

 const is array =  array.is array( array);
  • fill() - 用指定值填充数组的所有元素

 array.fill(0);
  • findIndex() - 查找满足条件的第一个元素的索引

 const foundIndex =  array.findIndex(item => item === 3);
  • keys() - 返回包含数组所有索引的迭代器

 const keysIterator =  array.keys();
  • values() - 返回包含数组所有值的迭代器

 const valuesIterator =  array.values();
  • entries() - 返回包含数组所有条目的迭代器(索引和值)

 const entriesIterator =  array.entries();
  • flat() - 将嵌套的数组扁平化为指定层级的新数组

 const nested array = [1, [2, [3]]];
 const flat array = nested array.flat(2);
  • from() - 从类似数组或可迭代对象创建新数组

 const new arrayFrom =  array.from("hello");
  • is array() - 检查是否为数组

 const is array2 =  array.is array( array);
  • toString() - 将数组转换为字符串

 const  arrayToString =  array.toString();
  • indexOf() - 查找元素在数组中首次出现的索引

 const index2 =  array.indexOf(4);
  • copyWithin() - 在数组内部复制元素到指定位置

 array.copyWithin(0, 2);
  • includes() - 检查数组是否包含指定元素

 const includesElement2 =  array.includes(3);
  • flatMap() - 首先映射每个元素,然后扁平化结果为新数组

 const flatMapped array =  array.flatMap(item => [item, item * 2]);
  • reduceRight() - 从数组右侧开始进行累积计算

 const sumRight =  array.reduceRight((acc, item) => acc + item, 0);
  • some() - 检查数组是否存在满足条件的元素

 const hasEvenNumber =  array.some(item => item % 2 === 0);
  • values() - 返回包含数组所有值的迭代器

 const valuesIterator2 =  array.values();
  • keys() - 返回包含数组所有索引的迭代器

 const keysIterator2 =  array.keys();
  • entries() - 返回包含数组所有条目的迭代器(索引和值)

 const entriesIterator2 =  array.entries();
  • lastIndexOf() - 查找元素在数组中最后一次出现的索引

 const lastIndex2 =  array.lastIndexOf(3);
  • fill() - 用指定值填充数组的所有元素

 array.fill(0);
  • pop() - 移除并返回数组末尾的元素

 const poppedElement2 =  array.pop();
  • reduce() - 使用回调函数对数组元素进行累积计算

 const product =  array.reduce((acc, item) => acc * item, 1);
  • sort() - 对数组元素进行排序

 array.sort();
  • filter() - 使用回调函数筛选出满足条件的元素到新数组

 const evenNumbers =  array.filter(item => item % 2 === 0);
  • splice() - 修改数组,可用于插入、替换或删除元素

 array.splice(2, 1, 3.5);  //在索引2插入3.5,删除1个元素
  • push() - 在数组末尾添加一个或多个元素

 array.push(6);
  • reverse() - 反转数组元素的顺序

 array.reverse();
  • join() - 将数组元素以指定分隔符连接成字符串

const joinedString2 =  array.join('  ');
  • unshift() - 在数组开头添加一个或多个元素

 array.unshift(0);
  • find() - 使用回调函数查找满足条件的第一个元素

 const firstEvenNumber =  array.find(item => item % 2 === 0);
  • forEach() - 遍历数组的每个元素执行回调函数

 array.forEach(item => {
  console.log(item);
 });
  • map() - 使用回调函数映射数组的每个元素到新数组

 const squared array =  array.map(item => item ** 2);
  • shift() - 移除并返回数组开头的元素

 const shiftedElement2 =  array.shift();
  • flat() - 将嵌套的数组扁平化为一维的新数组

 const nested array2 = [1, [2, [3]]];
 const flattened array = nested array2.flat();
  • is array() - 检查是否为数组

 const is array3 =  array.is array( array);
  • every() - 检查数组所有元素是否都满足条件

 const allPositive =  array.every(item => item > 0);
  • findIndex() - 查找满足条件的第一个元素的索引

 const firstNegativeIndex =  array.findIndex(item => item < 0);
  • includes() - 检查数组是否包含指定元素

 const includesZero =  array.includes(0);
  • toString() - 将数组转换为字符串

 const  arrayToString2 =  array.toString();
  • reduceRight() - 从数组右侧开始进行累积计算

 const productRight =  array.reduceRight((acc, item) => acc * item, 1);
  • values() - 返回包含数组所有值的迭代器

 const valuesIterator3 =  array.values();
  • keys() - 返回包含数组所有索引的迭代器

 const keysIterator3 =  array.keys();
  • entries() - 返回包含数组所有条目的迭代器(索引和值)

 const entriesIterator3 =  array.entries();
  • copyWithin() - 在数组内部复制元素到指定位置

 array.copyWithin(1, 3);
  • lastIndexOf() - 查找元素在数组中最后一次出现的索引

 const lastIndexOf3 =  array.lastIndexOf(3);
  • splice() - 修改数组,可用于插入、替换或删除元素

 array.splice(2, 1, 3.5); // 在索引2插入3.5,删除1个元素
  • push() - 在数组末尾添加一个或多个元素

 array.push(6);
  • filter() - 使用回调函数筛选出满足条件的元素到新数组

 const oddNumbers =  array.filter(item => item % 2 !== 0);
  • sort() - 对数组元素进行排序

 array.sort();
  • unshift() - 在数组开头添加一个或多个元素

 array.unshift(0);
  • pop() - 移除并返回数组末尾的元素

 const poppedElement3 =  array.pop();
  • reduce() - 使用回调函数对数组元素进行累积计算

 const concatenatedString =  array.reduce((acc, item) => acc + item, '');
  • map() - 使用回调函数映射数组的每个元素到新数组

 const new arrayMapped =  array.map(item => item + 1);
  • find() - 使用回调函数查找满足条件的第一个元素

 const foundNumber =  array.find(item => item > 2);
  • includes() - 检查数组是否包含指定元素

 const includesThree =  array.includes(3);
  • shift() - 移除并返回数组开头的元素

 const shiftedElement3 =  array.shift();
  • every() - 检查数组所有元素是否都满足条件

 const allPositive2 =  array.every(item => item > 0);
  • some() - 检查数组是否存在满足条件的元素

 const hasPositiveNumber =  array.some(item => item > 0);
  • join() - 将数组元素以指定分隔符连接成字符串

const joinedString3 =  array.join('');
  • concat() - 合并多个数组为一个新数组

 const merged array =  array.concat([6, 7, 8]);
  • reverse() - 反转数组元素的顺序

 array.reverse();
  • slice() - 截取数组的一部分,返回一个新数组

 const sliced array2 =  array.slice(1, 4);
  • findIndex() - 查找满足条件的第一个元素的索引

 const firstIndex =  array.findIndex(item => item === 3);
  • is array() - 检查是否为数组

 const is array4 =  array.is array( array);
  • keys() - 返回包含数组所有索引的迭代器

 const keysIterator4 =  array.keys();
  • values() - 返回包含数组所有值的迭代器

 const valuesIterator4 =  array.values();
  • entries() - 返回包含数组所有条目的迭代器(索引和值)

 const entriesIterator4 =  array.entries();
  • flat() - 将嵌套的数组扁平化为一维的新数组

 const deeplyNested array = [1, [2, [3, [4]]]];
 const flat array2 = deeplyNested array.flat(Infinity);
  • from() - 从类似数组或可迭代对象创建新数组

 const  arrayFromSet =  array.from(new Set([1, 2, 3]));
  • is array() - 检查是否为数组

 const is array5 =  array.is array( array);
  • toString() - 将数组转换为字符串

 const  arrayToString3 =  array.toString();
  • copyWithin() - 在数组内部复制元素到指定位置

 array.copyWithin(0, 2);
  • includes() - 检查数组是否包含指定元素

 const includesFour =  array.includes(4);
  • find() - 使用回调函数查找满足条件的第一个元素

 const firstPositiveNumber =  array.find(item => item > 0);
  • reduceRight() - 从数组右侧开始进行累积计算

 const productRight2 =  array.reduceRight((acc, item) => acc * item, 1);
  • some() - 检查数组是否存在满足条件的元素

 const hasPositiveEvenNumber =  array.some(item => item % 2 === 0);
  • values() - 返回包含数组所有值的迭代器

 const valuesIterator5 =  array.values();
  • keys() - 返回包含数组所有索引的迭代器

 const keysIterator5 =  array.keys();
  • entries() - 返回包含数组所有条目的迭代器(索引和值)

const entriesIterator5 =  array.entries();