当前位置:网站首页>The array technique, my love
The array technique, my love
2022-07-31 06:24:00 【Forevermoremo】
Foreword
Arrays are one of the most common concepts in Javascript and offer us many possibilities for manipulating data.Proper array handling techniques can make our coding more efficient, so let's take a look.
1. Array deduplication
Favorite question in front-end interviews, hahaha.
"Determine whether a simple array element is repeated
function isRepeat(arr) {var hash = {};for (var i in arr) {if (hash[arr[i]]) {return true;}hash[arr[i]] = true;}return false;}①Using ES6 Set to deduplicate
You can use it regardless of compatibility.
Note: When the content of the object is the same, because the storage address is different, it is not removed.
function unique (arr) {return Array.from(new Set(arr)); //of course there is a simpler way to write it: [...new Set(arr)]}var arr = [1,1,undefined,undefined,null,null,NaN,NaN,{},{}];console.log(unique(arr))//[1,undefined,null,NaN,{},{}]②Using indexOf to remove duplicates
The interviewer asked me to use handwriting as my favorite method to remove duplicates from arrays. Of course, splice can also be chosen to remove duplicates.
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;}There are many methods, here are two.
2, empty array
var arr= ["a", "b", "c"];arr.length = 0;3, merge array
①concat
is used to combine two or more arrays.This method does not change the existing array, but returns a new array.
var num1 = [1, 2, 3],num2 = [4, 5, 6],num3 = [7, 8, 9];var nums = num1.concat(num2, num3); ②ES6 spread operator (…)
var num1 = [1, 2, 3],num2 = [4, 5, 6],num3 = [7, 8, 9];var nums = [...num1, ...num2, ...num3];4. Conversion between strings and arrays
①String to array split
Slice the string with the specified delimiter and convert it into several array elements.
var str = '123,456,789';var strArr = str.split(',');split application: commonly used when specifying parameters after getting the 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; //return the parameter value as an object} ②Array to String join
Concatenate the elements of the array with the specified delimiter and convert it into a string.
var strArr = ['abc', 'def', 'hig'];var str = strArr.join(','); //When the delimiter is ',', it is consistent with the result of toString5. Delete the array elements that meet the conditions
filter
Runs the given function on each item of the array, returning an array of items whose result is true.
var arr = ["apple", "orange","happy"];var arrRes = arr. filter(function(v){return v.length > 5;});边栏推荐
- VS2017 connects to MYSQL
- Navicat从本地文件中导入sql文件
- Tensorflow相关list
- mPEG-DSPE 178744-28-0 Methoxy-polyethylene glycol-phosphatidylethanolamine linear PEG phospholipids
- 为数学而歌之伯努利家族
- 解决background-size:cover时图片铺满但显示不完整?
- Introduction to CLS-PEG-FITC Fluorescein-PEG-CLS Cholesterol-PEG-Fluorescein
- Evaluating Machine Learning Models - Excerpt
- 科研试剂Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺
- Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....
猜你喜欢

The browser looks for events bound or listened to by js

活体检测CDCN学习笔记

ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘

MySQL master-slave switching steps

Cholesterol-PEG-Azide CLS-PEG-N3 胆固醇-聚乙二醇-叠氮 MW:3400

Pytorch实现ResNet

Phospholipids-Polyethylene Glycol-Active Esters for Scientific Research DSPE-PEG-NHS CAS: 1445723-73-8

WeChat applet source code acquisition and decompilation method

pytorch模型微调finetuning训练image_dog(kaggle)

Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
随机推荐
Numpy常用函数
softmax函数详解
ROS之service编程的学习和理解
Tensorflow——演示
SSH automatic reconnection script
YOLOX中的SimOTA
mPEG-DSPE 178744-28-0 甲氧基-聚乙二醇-磷脂酰乙醇胺线性PEG磷脂
计算图像数据集均值和方差
这些数组技巧,我爱了
Multi-Modal Face Anti-Spoofing Based on Central Difference Networks学习笔记
VTK环境配置
Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
DSPE-PEG-Azide DSPE-PED-N3 Phospholipid-Polyethylene Glycol-Azide Lipid PFG
The browser looks for events bound or listened to by js
Pytorch学习笔记09——多分类问题
为数学而歌之伯努利家族
VS connects to MYSQL through ODBC (1)
DSPE-PEG-Thiol DSPE-PEG-SH 磷脂-聚乙二醇-巯基脂质体制备用
wangeditor富文本编辑器上传图片以及跨域问题解决
MW:3400 4-Arm PEG-DSPE 四臂-聚乙二醇-磷脂一种饱和的18碳磷脂