js 怎样用dropRight移除数组的后n个元素

js 怎样用dropRight移除数组的后n个元素
最新回答
单纯的想你

2021-06-18 06:55:37

在 JavaScript 中,若要移除数组末尾的 n 个元素,可通过以下方法实现,每种方法各有适用场景:

1. 使用 Array.prototype.slice()(推荐:创建新数组,保持不可变性)
  • 原理:通过指定起始索引(0)和结束索引(原数组长度 - n),截取新数组。
  • 特点

    不修改原数组,返回新数组,符合函数式编程的不可变性原则。

    若 n 大于等于数组长度,返回空数组。

  • 示例:const originalArray = [1, 2, 3, 4, 5, 6, 7];const n = 3;const newArray = originalArray.slice(0, originalArray.length - n);console.log(newArray); // 输出: [1, 2, 3, 4]console.log(originalArray); // 输出: [1, 2, 3, 4, 5, 6, 7](原数组未变)
2. 使用 Array.prototype.splice()(直接修改原数组)
  • 原理:从指定索引开始删除指定数量的元素,并返回被删除的元素。
  • 特点

    直接修改原数组,需谨慎使用以避免副作用。

    返回被移除的元素数组。

  • 示例:const originalArrayMutable = [10, 20, 30, 40, 50, 60, 70];const nToSplice = 2;const removedElements = originalArrayMutable.splice( originalArrayMutable.length - nToSplice, nToSplice);console.log(originalArrayMutable); // 输出: [10, 20, 30, 40, 50](原数组被修改)console.log(removedElements); // 输出: [60, 70](被移除的元素)
3. 使用 Lodash 的 _.dropRight()(语义化操作,需引入库)
  • 原理:封装了“移除末尾 n 个元素”的逻辑,提供直观的 API。
  • 特点

    返回新数组,不修改原数组。

    需安装 Lodash 库(npm install lodash)。

  • 示例:import _ from 'lodash';const lodashArray = ['a', 'b', 'c', 'd', 'e'];const nLodash = 2;const droppedArray = _.dropRight(lodashArray, nLodash);console.log(droppedArray); // 输出: ['a', 'b', 'c']console.log(lodashArray); // 输出: ['a', 'b', 'c', 'd', 'e'](原数组未变)
4. 使用 Array.prototype.filter()(声明式过滤,基于索引)
  • 原理:通过回调函数过滤掉索引大于等于 原数组长度 - n 的元素。
  • 特点

    返回新数组,不修改原数组。

    代码可读性强,适合基于条件的过滤。

  • 示例:const data = [10, 20, 30, 40, 50, 60];const dropCount = 2;const filteredArray = data.filter((_, index) => index < data.length - dropCount);console.log(filteredArray); // 输出: [10, 20, 30, 40]
5. 使用 Array.prototype.reduce()(灵活聚合,但冗长)
  • 原理:通过累加器逐步构建新数组,仅保留索引小于 原数组长度 - n 的元素。
  • 特点

    返回新数组,不修改原数组。

    适合复杂聚合场景,但简单截断时显得冗余。

  • 示例:const items = ['apple', 'banana', 'cherry', 'date', 'elderberry'];const numToRemove = 2;const reducedArray = items.reduce((accumulator, currentValue, currentIndex, array) => { if (currentIndex < array.length - numToRemove) { accumulator.push(currentValue); } return accumulator;}, []);console.log(reducedArray); // 输出: ['apple', 'banana', 'cherry']
6. 传统 for 循环(基础控制,性能优化)
  • 原理:手动遍历数组,将不需要移除的元素添加到新数组中。
  • 特点

    返回新数组,不修改原数组。

    适合对性能有极致要求的场景(但现代 JS 引擎中差异微小)。

  • 示例:const original = [1, 2, 3, 4, 5];const count = 2;const result = [];for (let i = 0; i < original.length - count; i++) { result.push(original[i]);}console.log(result); // 输出: [1, 2, 3]
性能与选择策略
  • 时间复杂度:所有方法均为 O(n),需遍历数组。
  • 空间复杂度

    slice()、filter()、reduce()、_.dropRight():O(n)(创建新数组)。

    splice():O(1)(原地修改,但返回被移除元素的数组占用 O(n) 空间)。

  • 选择建议

    优先使用 slice() 或 _.dropRight():代码可读性强,符合不可变性原则。

    需修改原数组时用 splice():但需谨慎管理副作用。

    极端性能场景:通过性能分析确认瓶颈后,考虑手动循环或类型化数组优化。