For details about the common methods of array in javascript, javascriptarray
Common Methods of array in javascript
Recently I have summarized some common methods in array,
Most of the methods are from the JavaScript Framework Design book,
If there are better methods or other commonly used methods for string, I hope you will not be enlightened.
Part 1
Array deduplication summarizes some array deduplication methods. The Code is as follows:
/*** Deduplication, ordered state * @ param target * @ returns {Array} */function unique (target) {let result = []; loop: for (let I = 0, n = target. length; I <n; I ++) {for (let x = I + 1; x <n; x ++) {if (target [x] === target [I]) {continue loop ;}} result. push (target [I]);} return result;}/*** deduplication, unordered, highest efficiency * @ param target * @ returns {Array} */function unique1 (target) {let obj ={}; for (let I = 0, n = target. length; I <n; I ++) {obj [target [I] = true;} return Object. keys (obj);}/*** ES6 statement, ordered state * @ param target * @ returns {Array} */function unique2 (target) {return Array. from (new Set (target);} function unique3 (target) {return [... new Set (target)];}
Part 2
Obtain the value in the array, including the maximum value, minimum value, and random value.
/*** Returns the minimum value in the array for the number array * @ param target * @ returns {*} */function min (target) {return Math. min. apply (0, target);}/*** returns the maximum value in the array for the number array * @ param target * @ returns {*} */function max (target) {return Math. max. apply (0, target);}/*** randomly selects an element from the array * @ param target * @ returns {*} */function random (target) {return target [Math. floor (Math. random () * target. length)];}
Part 3
Operations on the array itself, including removing values, shuffling, flattening, and filtering nonexistent values
/*** Remove the element at the specified position in the array and return a boolean indicating whether the operation is successful * @ param target * @ param index * @ returns {boolean} */function removeAt (target, index) {return !! Target. splice (index, 1 ). length;}/*** removes the first element in the array that matches the input parameter, if a boolean value is returned, it indicates whether the request is successful * @ param target * @ param item * @ returns {boolean} */function remove (target, item) {const index = target. indexOf (item); if (~ Index) {return removeAt (target, index);} return false ;} /*** shuffling the array * @ param array * @ returns {array} */function shuffle (array) {let m = array. length, t, I; // While there remain elements to shuffle... While (m) {// Pick a remaining element... I = Math. floor (Math. random () * m --); // And swap it with the current element. t = array [m]; array [m] = array [I]; array [I] = t;} return array;}/*** flat the array, returns a one-dimensional new Array * @ param target * @ returns {Array} */function flatten (target) {let result = []; target. forEach (function (item) {if (Array. isArray (item) {result = result. concat (flatten (item);} else {result. push (item) ;}}); return result ;}/ * ** Filter the null and undefined attributes without affecting the original Array * @ param target * @ returns {Array. <T> | *} */function compat (target) {return target. filter (function (el) {return el! = Null ;})}
Part 4
Operate the Array Based on the specified conditions.
/*** Grouping based on a specified condition (such as a callback or an attribute of an object) to return an object. * @ Param target * @ param val * @ returns {{}} */function groupBy (target, val) {var result ={}; var iterator = isFunction (val )? Val: function (obj) {return obj [val] ;}; target. forEach (function (value, index) {var key = iterator (value, index); (result [key] | (result [key] = []). push (value) ;}); return result ;}function isFunction (obj) {return Object. prototype. toString. call (obj) = '[object Function]';} // example function iterator (value) {if (value> 10) {return 'a ';} else if (value> 5) {return 'B';} return 'C';} var target = [,]; Console. log (groupBy (target, iterator);/*** get the specified attribute of each element in the object array, returns * @ param target * @ param name * @ returns {Array} */function pluck (target, name) {let result = [], prop; target. forEach (function (item) {prop = item [name]; if (prop! = Null) {result. push (prop) ;}}); return result ;}/ *** sort by specified conditions, usually used for object arrays * @ param target * @ param fn * @ param scope * @ returns {Array} */function sortBy (target, fn, scope) {let array = target. map (function (item, index) {return {el: item, re: fn. call (scope, item, index )};}). sort (function (left, right) {let a = left. re, B = right. re; return a <B? -1: a> B? 1: 0;}); return pluck (array, 'El ');}
Part 5
Array union, intersection, and difference set.
/*** Obtain the union set of two arrays * @ param target * @ param array * @ returns {Array} */function union (target, array) {return unique (target. concat (array);}/*** ES6 Union * @ param target * @ param array * @ returns {Array} */function union1 (target, array) {return Array. from (new set()..tar get ,... array]);}/*** returns the intersection of two arrays * @ param target * @ param array * @ returns {Array. <T> | *} */function intersect (target, array) {return targ Et. filter (function (n) {return ~ Array. indexOf (n);})}/*** ES6 intersection * @ param target * @ param array * @ returns {Array} */function intersect1 (target, array) {array = new Set (array); return Array. from (new set()..tar get]. filter (value => array. has (value);}/*** difference set * @ param target * @ param array * @ returns {ArrayBuffer | Blob | Array. <T> | string} */function diff (target, array) {var result = target. slice (); for (var I = 0; I <result. length; I ++) {for (var j = 0; j <array. length; j ++) {if (result [I] = array [j]) {result. splice (I, 1); I --; break; }}return result ;} /*** ES6 difference Set * @ param target * @ param array * @ returns {Array} */function diff1 (target, array) {array = new Set (array ); return Array. from (new set()..tar get]. filter (value =>! Array. has (value ))));}
Part 6
The array contains the specified target.
/*** Determine whether the array contains the specified target * @ param item * @ returns {boolean} */function contains (target, item) {return target. indexOf (item)>-1 ;}
Finally, simulate the implementation principles of pop, oush, shift, and unshift in the array.
const _slice = Array.prototype.slice;Array.prototype.pop = function() { return this.splice(this.length - 1,1)[0];};Array.prototype.push = function() { this.splice.apply(this,[this.length,0].concat(_slice.call(arguments))); return this.length;};Array.prototype.shift = function() { return this.splice(0,1)[0];};Array.prototype.unshift = function() { this.splice.apply(this, [0,0].concat(_slice.call(arguments))); return this.length;};
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!