第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

熱點(diǎn)面試題: Array中有哪些非破壞性方法?

前言

极度投入,深度沉浸,边界清晰

前端小菜鸡一枚,分享的文章纯属个人见解,若有不正确或可待讨论点可随意评论,与各位同学一起学习~

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()

  • 用法:
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']
  • 实现:
/**
 * 测试
 */
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()

  • 用法:
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']
  • 实现:
/**
 * 测试
 */
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
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']
  • 实现:
/**
 * 测试
 */
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, '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()

  • 用法:
    .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]
  • 实现:
/**
 * 测试
 */
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;
    };
}

文章特殊字符描述:

  1. 问题标注 Q:(question)
  2. 答案标注 R:(result)
  3. 注意事项标准:A:(attention matters)
  4. 详情描述标注:D:(detail info)
  5. 总结标注:S:(summary)
  6. 分析标注:Ana:(analysis)
  7. 提示标注:T:(tips)
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
Web前端工程師
手記
粉絲
6
獲贊與收藏
41

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 1
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消