当前位置:网站首页>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;});边栏推荐
猜你喜欢
随机推荐
Redis-哈希
cocos2d-x-3.2 create project method
ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘
活体检测CDCN学习笔记
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.
浏览器中的画中画(Picture-in-Picture)API
SSH automatic reconnection script
Evaluating Machine Learning Models - Excerpt
[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config
科研试剂Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺
虚拟机查看端口号进程
禅道安装及使用教程
opencv之图像二值化处理
Cholesterol-PEG-Azide CLS-PEG-N3 胆固醇-聚乙二醇-叠氮 MW:3400
CAS:474922-22-0 Maleimide-PEG-DSPE 磷脂-聚乙二醇-马来酰亚胺简述
Fluorescein-PEG-DSPE 磷脂-聚乙二醇-荧光素荧光磷脂PEG衍生物
mPEG-DMPE Methoxy-polyethylene glycol-bismyristyl phosphatidylethanolamine for stealth liposome formation
cv2.resize()是反的
Pytorch每日一练——预测泰坦尼克号船上的生存乘客
数据预处理、特征工程和特征学习-摘抄









