Common JavaScript array operations (1)


Foreword array is one of the common data types in JavaScript. I will make a brief record and summary of its operation methods here.This article mainly includes: create an array to determine whether the array class array and array conversion array to heavy you can choose to eat according to their own needs.Creating an array is a basic skill, which mainly includes the following methods:Const arr = [1,2,3] // array literal const arr = [,,,] // hole array const arr =New Array(4) // [,,,,]const arr = new Array(4,2) // [4,2]const arr = array.of (1,2,3)// [1,2,3] the most commonly used method is array literals.The main ways to check whether an array is an array are as follows:/ / methods [1, 2, 3] instanceof Array / / method 2 [1, 2, 3]. The constructor = = = Array / / method three Object. The prototype. ToString. Call = = = ([1, 2, 3])& # 39;[object Array]'Function isArray(arr){return array.isarray?Array.isArray(arr):Object.prototype.toString.call(arr) === '[object Array]'} The most commonly used method is the isArray method.Class we sometimes run into arrays and conversion is not pure array data structure, generally it is classified as “array”, a class can use the following method to pure array: an array const x = document. QuerySelectorAll (& # 39;a');/ / method a Array. Prototype. Slice. Call (x);Array.from(x);Array.from(x,mapFn,thisArg);Function toArray(x){let res = [] for(item of x){res.push(item)} return res}//Array.apply(null,x)// Array.apply([],x) [].concate.apply ([],x)These parameters are then passed to the called method (new Array or concat).Array decrement essentially requires that two elements are compared for equality, and if they are equal, one element is discarded.For accurate judgment, Object. Is is used for comparison.1) Use a set to duplicate elements, so convert an array to a set and then convert it back to an array.Function unique(arr){return array. from(new Set(arr)) // return […new Set(arr)]}2)The inner layer loops through all elements after the current element, and splice removes one element if it finds equality.Function unique(arr){for(let I = 0;i < arr.length;i++){ for(let j = i + 1;i < arr.length;j++){ if(Object.is(arr[i],arr[j])){ arr.splice(j,1) j-- } } } return arr} 4) new array + includes check if the element is already in the array before adding it to the array:Function unique(arr){const res = [] arr.foreach ((item,index) => {// If (res.indexof (item) == -1),But NaN if(!Include (item)){res.push(item)}})}4) reduce + includesfunction unique(arr){returnarr.reduce((acc,cur) => { // return acc.includes(cur) ? acc : acc.concat(cur) return acc.includes(cur) ? Acc: […acc,cur]},[])}5) Create an array + sort by calling toStrng on each element and then sorting at the string level.Function unique(arr){arr.sort() const res = [arr[0]] for(let I = 1;i < arr.length;i++){ if(!Object. Is (arr[I],arr[i-1])){res.push(arr[I])}} return res}6) Create array + create array +Includes ".Create an array and check whether the element is already an object property before adding it to the array:Function unique(arr){const res = [] const obj = {} arr.foreach ((item,index) => {if(!obj[item]){ res.push(item) obj[item] = 1 } else { obj[item]++ } }) Return res} obj[true] is equal to obj[“true”].7) Using a map is essentially the same as above, but without creating a new array:function unique(arr){ let map = new Map() for(item of arr){ if(!Map.has (item)){map.set(item,true)}} return […map.keys()]}8) filter + indexOfKeep elements whose index is the same as the index when they first appear. Such elements can be filtered by filter and placed in an array:Function unique(arr){return arr.filter((item,index) => index === arr.indexof (item))}So that’s a summary of some of the basic operations associated with arrays.~~ This article is over, thanks for reading!Learn interesting knowledge, meet interesting friends, build interesting soul!Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!

Leave a Reply

Your email address will not be published.