前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >热点面试题: Array中有哪些非破坏性方法?

热点面试题: Array中有哪些非破坏性方法?

作者头像
Ims
发布2024-03-13 14:58:53
520
发布2024-03-13 14:58:53
举报

Array中有哪些非破坏性方法?

  • 非破坏性方法:调用的时不会改变原始的数组:例如:filter、some、map、find、join、concat、forEach、every、reduce、flat、slice
  • 破坏性方法:与破坏性方法相反,例如:sort、reverse、splice、push、pop、shift、unshift
  • 新的数组非破坏性方法toSorted、toReversed、with、toSpliced

1. toSorted()

  • • 用法:
代码语言:javascript
复制
const array = ['c', 'o', 'n', 'a', 'r', 'd', 'l', 'i'];
const result = array.toSorted();
console.log(result); //  ['a', 'c', 'd', 'i', 'l', 'n', 'o', 'r']
console.log(array); // ['c', 'o', 'n', 'a', 'r', 'd', 'l', 'i']
  • • 实现:
代码语言:javascript
复制
/**
 * 测试
 */
const arr = [1, 5, 8, 1, 5, 1, 8, 1, 8, 4, 31, 8, 4, 48, 751, 81, 1, 5, 7, 1, 5, 3, 5];
if (!Array.prototype.toSorted) {
    Array.prototype.toSorted = function (compareFn) {
        return this.slice().sort(compareFn);
    };
}
let res = arr.toSorted((a, b) => a - b);
console.log(1111, res); // [1, 1, 1,  1,  1,  1,   3, 4,4, 5, 5,  5,  5,  5,   7, 8,8, 8, 8, 31, 48, 81, 751 ]

// 实现
/**
 * compareFn:排序的方法:
 *  例如:
 *  升序:(a, b) => a - b
 *  降序:(a, b) => b - a
 */
if (!Array.prototype.toSorted) {
    Array.prototype.toSorted = function (compareFn) {
        return this.slice().sort(compareFn);
    };
}

2. toReversed()

  • • 用法:
代码语言:javascript
复制
const array = ['c', 'o', 'n', 'a', 'r', 'd', 'l', 'i'];
const result = array.toReversed();
console.log(result); //  ['i', 'l', 'd', 'r', 'a', 'n', 'o', 'c']
console.log(array); // ['c', 'o', 'n', 'a', 'r', 'd', 'l', 'i']
  • • 实现:
代码语言:javascript
复制
/**
 * 测试
 */
const arr = [1, 5, 8, 1, 5, 1, 8, 1, 8, 4, 31, 8, 4, 48, 751, 81, 1, 5, 7, 1, 5, 3, 5];
if (!Array.prototype.toReversed) {
    Array.prototype.toReversed = function () {
        return this.slice().reverse();
    };
}
let res = arr.toReversed();
console.log(1111, res); // [5,  3, 5, 1,  7, 5, 1, 81,751, 48, 4, 8, 31, 4, 8,  1,8,  1, 5, 1,  8, 5, 1 ]

// 实现
if (!Array.prototype.toReversed) {
    Array.prototype.toReversed = function () {
        return this.slice().reverse();
    };
}

3. with()

  • • 用法:array[index] = value
代码语言:javascript
复制
const array = ['c', 'o', 'n', 'a', 'r', 'd', 'l', 'i'];
const result = array.with(0, 'ConardLi');
console.log(result); //  ['ConardLi', 'o', 'n', 'a', 'r', 'd', 'l', 'i'];
console.log(array); // ['c', 'o', 'n', 'a', 'r', 'd', 'l', 'i']
  • • 实现:
代码语言:javascript
复制
/**
 * 测试
 */
const arr = [1, 5, 8, 1, 5, 1, 8];
if (!Array.prototype.with) {
    Array.prototype.with = function (index, value) {
        const copy = this.slice();
        copy[index] = value;
        return copy;
    };
}
let res = arr.with(4, 'xxx');
console.log(1111, res); // [1, 5, 8, 1, 5, 'xxx', 1, 8,]
console.log(2222, arr); // [1, 5, 8, 1, 5, 1, 8,]

// 实现
if (!Array.prototype.with) {
    Array.prototype.with = function (index, value) {
        const copy = this.slice();
        copy[index] = value;
        return copy;
    };
}

4. toSpliced()

  • • 用法:
代码语言:javascript
复制
    .toSpliced(start, deleteCount, ...items)

-   它从 start 开始删除 deleteCount 个元素 ;
-   然后把 items 插入到被删除的位置;
-   最后返回已删除的元素。

const array = [1, 2, 3, 4, 5, 6];
const result = array.splice(1, 2, 0);
console.log(result); //  [2, 3]
console.log(array);  // [1, 0, 4, 5, 6]
  • • 实现:
代码语言:javascript
复制
/**
 * 测试
 */
const arr = [1, 5, 8, 1, 5, 1, 8];
if (!Array.prototype.toSpliced) {
    Array.prototype.toSpliced = function (start, deleteCount, ...items) {
        const copy = this.slice();
        copy.splice(start, deleteCount, ...items);
        return copy;
    };
}
let res = arr.toSpliced(1, 2, 0); // [ 1, 0, 1, 5, 1, 8 ]
console.log(1111, res);

// 实现
if (!Array.prototype.toSpliced) {
    Array.prototype.toSpliced = function (start, deleteCount, ...items) {
        const copy = this.slice();
        copy.splice(start, deleteCount, ...items);
        return copy;
    };
}

文章特殊字符描述

•问题标注 Q:(question)•答案标注 R:(result)•注意事项标准:A:(attention matters)•详情描述标注:D:(detail info)•总结标注:S:(summary)•分析标注:Ana:(analysis)•提示标注:T:(tips)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 非同质前端札记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Array中有哪些非破坏性方法?
    • 1. toSorted()
      • 2. toReversed()
        • 3. with()
          • 4. toSpliced()
            • 文章特殊字符描述
            相关产品与服务
            腾讯云服务器利旧
            云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档