当前位置:网站首页>编程题:移除数组中的元素(JS实现)
编程题:移除数组中的元素(JS实现)
2022-06-22 06:51:00 【webchang】
一、要求直接在原数组上操作
题目描述:移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回。
输入:[1, 2, 2, 3, 4, 2, 2], 2
输出:[1, 3, 4]
方法1:
正向遍历数组,当数组元素的值与 item 相等时,就使用 splice 方法将其去除,但是要注意下标值的问题,去除一个数组元素后,下标值要减1,这样才能遍历到所有的元素。
function removeWithoutCopy(arr, item) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === item) {
arr.splice(i, 1);
i--; // 注意这里
}
}
return arr;
}
方法2:
反向遍历数组,当数组元素的值与 item 相等时,就使用 splice 方法将其去除。和正向遍历不同的是,这里没有下标值错乱的问题。
function removeWithoutCopy(arr, item) {
for (let i = arr.length - 1;i >= 0;i--) {
if (arr[i] === item) {
arr.splice(i, 1);
}
}
return arr;
}
方法3:
使用 while 循环
function removeWithoutCopy(arr, item) {
while (arr.indexOf(item) !== -1) {
arr.splice(arr.indexOf(item), 1);
}
return arr;
}
二、 要求不能直接修改数组 arr,结果返回新的数组
题目描述:移除数组 arr 中的所有值与 item 相等的元素。不要直接修改数组 arr,结果返回新的数组。
输入:[1, 2, 2, 3, 4, 2, 2], 2
输出:[1, 3, 4]
方法1:
使用 filter 方法
function remove(arr, item) {
return arr.filter(num => {
return num !== item;
})
}
方法2:
使用普通的 for 循环或者 forEach 或者 for of,将和 item 不相等的元素放入一个新的数组中。
function remove2(arr, item) {
let result = [];
// for (let i = 0; i < arr.length; i++) {
// if (arr[i] !== item) {
// result.push(arr[i]);
// }
// }
// arr.forEach(num => {
// if (num !== item) {
// result.push(num);
// }
// });
for (let num of arr) {
if (num !== item) {
result.push(num);
}
}
return result;
}
方法3:
先使用 set 把原数组去重,这样数组中只剩下一个item了,再找到item的下标,使用splice去除。
function remove3(arr ,item) {
let result = [...new Set(arr)];
result.splice(result.indexOf(item),1);
return result;
}
前端学习交流QQ群,群内学习讨论的氛围很好,大佬云集,期待您的加入:862748629 点击加入
边栏推荐
- golang调用sdl2,播放pcm音频,报错signal arrived during external code execution。
- 【5G NR】NGAP协议之NG Setup
- Xh_ CMS penetration test documentation
- [5g NR] RRC connection reconstruction analysis
- import keras时遇到的错误 TypeError: Descriptors cannot not be created directly. If this call came from a _
- Laravel excel 3.1 column width setting does not work
- [openairinterface5g] RRC NR resolution (I)
- Iframe framework, native JS routing
- [PHP] composer 安装
- Blog add mailbox private message shortcut
猜你喜欢

Introduction to 51 Single Chip Microcomputer -- digital clock

5g terminal identification Supi, suci and IMSI analysis

ReadWriteLock

Wildfire stm32f407zgt6 learning notes beginner level chapter basic knowledge points

MySQL Niuke brush questions

Qt development simple Bluetooth debugging assistant (low power Bluetooth)
![[php]tp6 cli mode to create tp6 and multi application configurations and common problems](/img/19/0a3319b04fe6449c90ade6f27fca4a.png)
[php]tp6 cli mode to create tp6 and multi application configurations and common problems

vue连接mysql数据库失败
![[5g NR] RRC connection reconstruction analysis](/img/7a/6f9942b1874604664924e22e04d516.png)
[5g NR] RRC connection reconstruction analysis

Single cell literature learning (Part3) -- dstg: deconvoluting spatial transcription data through graph based AI
随机推荐
仙人掌之歌——进军To C直播(1)
Introduction to 51 single chip microcomputer - LED light
Callable
【5G NR】NAS连接管理—CM状态
Great progress in code
JDBC query result set, which is converted into a table
leetcode:面试题 08.12. 八皇后【dfs + backtrack】
Leetcode: interview question 08.12 Eight queens [DFS + backtrack]
The tidb community offline exchange meeting was seen by the partners from Tianjin and Shijiazhuang~
KV260的PMOD接口介绍
代码的巨大进步
Py之Optbinning:Optbinning的简介、安装、使用方法之详细攻略
5g-guti detailed explanation
6. 安装ssh连接工具(用于我们连接实验室的服务器)
Advanced usage of setting breakpoints during keil debugging
[5g NR] ng interface
Detailed tutorial on connecting MySQL with tableau
Why did I choose rust
EMC solutions
JDBC查询结果集,结果集转化成表