当前位置:网站首页>这些数组技巧,我爱了
这些数组技巧,我爱了
2022-07-31 05:17:00 【Forevermoremo】
前言
数组是Javascript最常见的概念之一,它为我们提供了处理数据的许多可能性。恰当的数组处理技巧能够让我们编码事半功倍,接下来我们就来看看吧。
1.数组去重
前端面试最爱问的问题,哈哈哈。
》判断简单数组元素是否重复
function isRepeat(arr) {
var hash = {};
for (var i in arr) {
if (hash[arr[i]]) {
return true;
}
hash[arr[i]] = true;
}
return false;
}①利用ES6 Set去重
不考虑兼容性就可以用哦。
注:当对象内容相同时,由于存储地址不一样,所以没有去除。
function unique (arr) {
return Array.from(new Set(arr)); //当然还有它更简单的写法:[...new Set(arr)]
}
var arr = [1,1,undefined,undefined,null,null,NaN,NaN,{},{}];
console.log(unique(arr))//[1,undefined,null,NaN,{},{}]②利用indexOf去重
面试官叫手写实现数组去重时本人最最最爱的方法,当然也可以选择splice去重。
function unique(arr) {
if (!Array.isArray(arr)) {
console.log('type error!');
return;
}
var array = [];
for (var i = 0; i < arr.length; i++) {
if (array.indexOf(arr[i]) === -1) {
array.push(arr[i]);
}
}
return array;
}还有很多的方法的,这里就写两种吧。
2、空数组
var arr= ["a", "b", "c"];
arr.length = 0;3、合并数组
①concat
用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
var num1 = [1, 2, 3],
num2 = [4, 5, 6],
num3 = [7, 8, 9];
var nums = num1.concat(num2, num3); ②ES6 扩展操作符(…)
var num1 = [1, 2, 3],
num2 = [4, 5, 6],
num3 = [7, 8, 9];
var nums = [...num1, ...num2, ...num3];4、字符串与数组的相互转换
①字符串转数组 split
用指定分隔符对字符串进行切片,转换为若干个数组元素。
var str = '123,456,789';
var strArr = str.split(',');split应用:常用于在获取url后指定参数时
function GetRequest() {
var url = location.search.replace(/\s+/g, ""),
theRequest = {};
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[decodeURI(strs[i].split("=")[0])]=decodeURI(strs[i].split("=")[1]);
}
}
return theRequest; //以对象方式返回参数值
} ②数组转字符串 join
用指定分隔符对数组元素进行拼接,转换为一个字符串。
var strArr = ['abc', 'def', 'hig'];
var str = strArr.join(','); //当分隔符为‘,’时,与toString结果一致5、对满足条件的数组元素进行删除
filter
对数组的每一项都运行给定的函数,返回 结果为 ture 的项组成的数组。
var arr = ["apple", "orange","happy"];
var arrRes = arr.filter(function(v){
return v.length > 5;
});
边栏推荐
猜你喜欢

Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400

CLS-PEG-FITC Fluorescein-PEG-CLS 胆固醇-聚乙二醇-荧光素简介

RuntimeError: CUDA error: no kernel image is available for execution on the device问题记录

为数学而歌之伯努利家族

Pytorch常用函数

活体检测FaceBagNet阅读笔记

Gradle sync failed: Uninitialized object exists on backward branch 142

Tensorflow steps on the pit while using it

Notes on creating a new virtual machine in Hyper-V

如何修改数据库密码
随机推荐
MySQL master-slave switching steps
Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400
wangeditor编辑器内容传至后台服务器存储
Software Testing Interview Questions 2021
After unicloud is released, the applet prompts that the connection to the local debugging service failed. Please check whether the client and the host are under the same local area network.
【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton
Tensorflow相关list
Fluorescein-PEG-DSPE Phospholipid-Polyethylene Glycol-Fluorescein Fluorescent Phospholipid PEG Derivatives
拒绝采样小记
ERROR Error: No module factory availabl at Object.PROJECT_CONFIG_JSON_NOT_VALID_OR_NOT_EXIST ‘Error
微信小程序源码获取与反编译方式
VS2017 connects to MYSQL
SSH automatic reconnection script
A simple bash to powershell case
化学试剂磷脂-聚乙二醇-氨基,DSPE-PEG-amine,CAS:474922-26-4
Cholesterol-PEG-Acid CLS-PEG-COOH Cholesterol-Polyethylene Glycol-Carboxyl Modified Peptides
Navicat从本地文件中导入sql文件
Podspec automatic upgrade script
Understanding of objects and functions in js
人脸识别AdaFace学习笔记