当前位置:网站首页>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;});边栏推荐
猜你喜欢

opencv之图像二值化处理

Tensorflow steps on the pit while using it

Global scope and function scope in js

Pytorch学习笔记13——Basic_RNN

DSPE-PEG-Biotin, CAS: 385437-57-0, phospholipid-polyethylene glycol-biotin prolongs circulating half-life

Navicat从本地文件中导入sql文件

OpenCV中的图像数据格式CV_8U定义

Xiaomi mobile phone SMS location service activation failed

Rejection sampling note

二进制转换成十六进制、位运算、结构体
随机推荐
Cholesterol-PEG-DBCO Cholesterol-Polyethylene Glycol-Diphenylcyclooctyne Chemical Reagent
Notes on creating a new virtual machine in Hyper-V
This in js points to the prototype object
Evaluating Machine Learning Models - Excerpt
MYSQL事务与锁问题处理
mPEG-DMPE 甲氧基-聚乙二醇-双肉豆蔻磷脂酰乙醇胺用于形成隐形脂质体
ERROR Error: No module factory availabl at Object.PROJECT_CONFIG_JSON_NOT_VALID_OR_NOT_EXIST ‘Error
MW:3400 4-Arm PEG-DSPE 四臂-聚乙二醇-磷脂一种饱和的18碳磷脂
浏览器中的画中画(Picture-in-Picture)API
2021年软件测试面试题大全
break and continue exit in js
MySQL 主从切换步骤
Phospholipids-Polyethylene Glycol-Active Esters for Scientific Research DSPE-PEG-NHS CAS: 1445723-73-8
自然语言处理相关list
cocos2d-x-3.2 image graying effect
Redis-哈希
Pytorch学习笔记13——Basic_RNN
Nmap的下载与安装
评估机器学习模型-摘抄
DSPE-PEG-Azide DSPE-PED-N3 磷脂-聚乙二醇-叠氮脂质PFG