热点面试题: 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, 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()
- 用法:
.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;
};
}
文章特殊字符描述:
- 问题标注
Q:(question)
- 答案标注
R:(result)
- 注意事项标准:
A:(attention matters)
- 详情描述标注:
D:(detail info)
- 总结标注:
S:(summary)
- 分析标注:
Ana:(analysis)
- 提示标注:
T:(tips)
往期回顾:
- 热点面试题:Virtual DOM 相关问题?
- 热点面试题:什么是粘包/半包问题,该如何解决?
- 热点面试题:console.log()同异步问题?
- 热点面试题:进程系列问题?
- 热点面试题:Node.js 中的垃圾回收机制?
- 热点面试题:简述 http3.0~http1.0 分别有什么改进?
- JavaScript中的AMD和CMD规范
- Vue数据监听Object.definedProperty()方法的实现
最后:
- 欢迎关注
『前端进阶圈』
公众号 ,一起探索学习前端技术...... - 公众号回复
加群
或扫码
, 即可加入前端交流学习群,长期交流学习...... - 公众号回复
加好友
,即可添加为好友
47 声望
2 粉丝
推荐阅读
【深度剖析】JavaScript中块级作用域与函数作用域
在 JavaScript 中,究竟是什么会生成一个新的作用域,只有函数才会生成新的作用域吗?那 JavaScript 其他结构能生成新的作用域吗?3.1 函数中的作用域
控心crazy阅读 127
安全地在前后端之间传输数据 - 「3」真的安全吗?
在「2」注册和登录示例中,我们通过非对称加密算法实现了浏览器和 Web 服务器之间的安全传输。看起来一切都很美好,但是危险就在哪里,有些人发现了,有些人嗅到了,更多人却浑然不知。就像是给门上了把好锁,还...
边城赞 31阅读 7.1k评论 5
涨姿势了,有意思的气泡 Loading 效果
今日,群友提问,如何实现这么一个 Loading 效果:这个确实有点意思,但是这是 CSS 能够完成的?没错,这个效果中的核心气泡效果,其实借助 CSS 中的滤镜,能够比较轻松的实现,就是所需的元素可能多点。参考我们...
chokcoco赞 20阅读 2k评论 2
在前端使用 JS 进行分类汇总
最近遇到一些同学在问 JS 中进行数据统计的问题。虽然数据统计一般会在数据库中进行,但是后端遇到需要使用程序来进行统计的情况也非常多。.NET 就为了对内存数据和数据库数据进行统一地数据处理,发明了 LINQ (L...
边城赞 17阅读 1.9k
【已结束】SegmentFault 思否写作挑战赛!
SegmentFault 思否写作挑战赛 是思否社区新上线的系列社区活动在 2 月 8 日 正式面向社区所有用户开启;挑战赛中包含多个可供作者选择的热门技术方向,根据挑战难度分为多个等级,快来参与挑战,向更好的自己前进!
SegmentFault思否赞 20阅读 5.6k评论 10
过滤/筛选树节点
又是树,是我跟树杠上了吗?—— 不,是树的问题太多了!🔗 相关文章推荐:使用递归遍历并转换树形数据(以 TypeScript 为例)从列表生成树 (JavaScript/TypeScript) 过滤和筛选是一个意思,都是 filter。对于列表来...
边城赞 18阅读 7.6k评论 3
Vue2 导出excel
2020-07-15更新 excel导出安装 {代码...} src文件夹下新建一个libs文件夹,新建一个excel.js {代码...} vue页面中使用 {代码...} ===========================以下为早期的文章今天在开发的过程中需要做一个Vue的...
原谅我一生不羁放歌搞文艺赞 14阅读 19.8k评论 9
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。