SoFunction
Updated on 2025-02-28

Detailed explanation of common methods about array in javascript

Common methods about array in javascript

Recently, I have summarized some common methods in array.

Most of these methods come from the book "JavaScript Framework Design".

If there is a better method, or other commonly used methods about string, I hope you will give me some advice.

Part One

Array deduplication, summarizes some methods for array deduplication, the code is as follows:

/**
  * Deduplication operation, ordered state
  * @param target
  * @returns {Array}
  */
function unique(target) {
  let result = [];
  loop: for (let i = 0,n = ;i < n; i++) {
    for (let x = i + 1;x < n;x++) {
      if (target[x] === target[i]) {
        continue loop;
      }
    }
    (target[i]);
  }
  return result;
}

/**
  * Deduplication operation, disordered state, highest efficiency
  * @param target
  * @returns {Array}
  */
function unique1(target) {
  let obj = {};
  for (let i = 0,n = ; i < n;i++) {
    obj[target[i]] = true;
  }
  return (obj);
}

/**
  * ES6 writing method, orderly state
  * @param target
  * @returns {Array}
  */
function unique2(target) {
  return (new Set(target));
}

function unique3(target) {
  return [...new Set(target)];
}

Part 2

Get the values ​​in the array, including the maximum, minimum, and random values.

/**
  * Returns the minimum value in the array for numeric arrays
  * @param target
  * @returns {*}
  */
function min(target) {
  return (0,target);
}

/**
  * Returns the maximum value in the array, used for array of numbers
  * @param target
  * @returns {*}
  */
function max(target) {
  return (0,target);
}

/**
  * Randomly select an element from the array
  * @param target
  * @returns {*}
  */
function random(target) {
  return target[(() * )];
}

Part 3

Operations on the array itself, including removing values, reshuffling, flattening and filtering non-existent values

/**
  * Removes the element in the specified position in the array, returning a boolean indicating success or not
  * @param target
  * @param index
  * @returns {boolean}
  */
function removeAt(target,index) {
  return !!(index,1).length;
}

/**
  * Remove the first element in the array that matches the parameter, and return the Boolean to indicate success or not.
  * @param target
  * @param item
  * @returns {boolean}
  */
function remove(target,item) {
  const index = (item);
  if (~index) {
    return removeAt(target,index);
  }
  return false;
}

/**
  * Shuffle the array
  * @param array
  * @returns {array}
  */
function shuffle(array) {
  let m = , t, i;
  // While there remain elements to shuffle…
  while (m) {
    // Pick a remaining element…
    i = (() * m--);

    // And swap it with the current element.
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }
  return array;
}



/**
  * Flatten the array and return a new one-dimensional array
  * @param target
  * @returns {Array}
  */
function flatten (target) {
  let result = [];
  (function(item) {
    if((item)) {
      result = (flatten(item));
    } else {
      (item);
    }
  });
  return result;
}


/**
  * Filter null and undefined in attributes, but does not affect the original array
  * @param target
  * @returns {Array.<T>|*}
  */
function compat(target) {
  return (function(el) {
    return el != null;
  })
}

Part 4

Operate the array according to the specified conditions.

/**
  * Group according to specified conditions (such as a callback or an attribute of the object) to form an object to return.
  * @param target
  * @param val
  * @returns {{}}
  */
function groupBy(target,val) {
  var result = {};
  var iterator = isFunction(val) ? val : function(obj) {
    return obj[val];
  };
  (function(value,index) {
    var key = iterator(value,index);
    (result[key] || (result[key] = [])).push(value);
  });
  return result;
}
function isFunction(obj){
  return (obj) === '[object Function]';
}

// examplefunction iterator(value) {
  if (value &gt; 10) {
    return 'a';
  } else if (value &gt; 5) {
    return 'b';
  }
  return 'c';
}
var target = [6,2,3,4,5,65,7,6,8,7,65,4,34,7,8];
(groupBy(target,iterator));



/**
  * Get the specified attributes of each element of the object array, and make the array return
  * @param target
  * @param name
  * @returns {Array}
  */
function pluck(target,name) {
  let result = [],prop;
  (function(item) {
    prop = item[name];
    if (prop != null) {
      (prop);
    }
  });
  return result;
}

/**
  * Sort according to specified conditions, usually used in object arrays
  * @param target
  * @param fn
  * @param scope
  * @returns {Array}
  */
function sortBy(target,fn,scope) {
  let array = (function(item,index) {
    return {
      el: item,
      re: (scope,item,index)
    };
  }).sort(function(left,right) {
    let a = , b = ;
    return a &lt; b ? -1 : a &gt; b ? 1 : 0;
  });
  return pluck(array,'el');
}

Part 5

union, intersection and difference of arrays.

/**
  * Take the merge of two arrays
  * @param target
  * @param array
  * @returns {Array}
  */
function union(target,array) {
  return unique((array));
}

/**
  * union of ES6
  * @param target
  * @param array
  * @returns {Array}
  */
function union1(target,array) {
  return (new Set([...target,...array]));
}

/**
  * Get the intersection of two arrays
  * @param target
  * @param array
  * @returns {Array.<T>|*}
  */
function intersect(target,array) {
  return (function(n) {
    return ~(n);
  })
}

/**
  * ES6 Intersection
  * @param target
  * @param array
  * @returns {Array}
  */
function intersect1(target,array) {
  array = new Set(array);
  return (new Set([...target].filter(value =&gt; (value))));
}

/**
  * Difference set
  * @param target
  * @param array
  * @returns {ArrayBuffer|Blob|Array.<T>|string}
  */
function diff(target,array) {
  var result = ();
  for (var i = 0;i &lt; ;i++) {
    for (var j = 0; j &lt; ;j++) {
      if (result[i] === array[j]) {
        (i,1);
        i--;
        break;
      }
    }
  }
  return result;
}

/**
  * ES6 difference set
  * @param target
  * @param array
  * @returns {Array}
  */
function diff1(target,array) {
  array = new Set(array);
  return (new Set([...target].filter(value =&gt; !(value))));
}

Part 6

The array contains the specified target.

/**
  * Determine whether the array contains the specified target
  * @param target
  * @param item
  * @returns {boolean}
  */
function contains(target,item) {
  return (item) &gt; -1;
}

Finally, simulate the implementation principles of pop, oush, shift and unshift in the array

const _slice = ;
 = function() {
  return ( - 1,1)[0];
};
 = function() {
  (this,[,0].concat(_slice.call(arguments)));
  return ;
};
 = function() {
  return (0,1)[0];
};
 = function() {
  (this,
    [0,0].concat(_slice.call(arguments)));
  return ;
};

Thank you for reading, I hope it can help you. Thank you for your support for this site!