How to implement random array sort?

  1. Use the sort method that comes with arrays
function randomSort(arr) {
  return arr.sort(() = > Math.random() - 0.5);
}
Copy the code
  1. Randomly extract array elements into the new array [math.floor () : round down]
function randomSort(arr) {
  let newArr = [];
  while (arr.length > 0) {
    let index = Math.floor(Math.random() * arr.length);
    newArr.push(arr[index]);
    arr.splice(index, 1);
  }
  return newArr;
}
Copy the code
  1. Shuffle: Swap the elements of an array randomly
function randomSort(arr) {
  for (let i = 0, len = arr.length; i < len; i++) {
    let index = Math.floor(Math.random() * len);
    [arr[index], arr[i]] = [arr[i], arr[index]];
  }
  return arr;
}
Copy the code

How to implement JS array deduplicate?

  1. ES6 Set Set: Similar to an array, each member has a unique value.
let arr = [1.2.3.4.3.2.3.4.6.7.6];
let unique = (arr) = > [...new Set(arr)];
let result = unique(arr);
console.log(result);   //[1, 2, 3, 4, 6, 7]
Copy the code

or

let arr = [1.2.3.4.4.5.5.6.7.7.8];
let result = [...new Set(arr)];
console.log(result);
Copy the code

or

let arr = [1.2.3.4.3.2.3.4.6.7.6];
let unique = (arr) = > Array.from(new Set(arr));
let result = unique(arr);
console.log(result); //[1, 2, 3, 4, 6, 7]
Copy the code
  1. The ES6 Map uses filter() to filter the array. The Map has() method is used to check whether duplicate keys exist in the Map. If so, false is returned to filter out the duplicate keys. If not, the value is used as the Map key and is set to 1.
let arr = [1.2.3.4.3.2.3.4.6.7.6];
let unique = (arr) = > {
    let seen = new Map(a);return arr.filter((item) = > {
        return! seen.has(item) && seen.set(item,1); })}let result = unique(arr);
console.log(result);   //[1, 2, 3, 4, 6, 7]
Copy the code
let arr = [1.2.3.4.4.5.5.6.7.7.8];
let temp = new Map(a);let result = arr.filter((item) = > {
  return! temp.has(item) && temp.set(item,1);
});
console.log(result);
Copy the code
  1. For double loop

Check whether the elements of the original array are the same as those of the new array. If they are, break out of the loop; otherwise, insert the elements of the original array into the new array.

let arr = [1.2.3.4.3.2.3.4.6.7.6];
let result = [];
for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < result.length; j++) {
        if (arr[i] === result[j]) {
            break; }}if(j == result.length) { result.push(arr[i]); }}console.log(result);   //[1, 2, 3, 4, 6, 7]

/* arr[0] = arr[0]; /* arr[0] = arr[0]; * /
Copy the code
  1. The indexOf() method returns the first occurrence of a specified element in an array. If not, return -1.
let arr = [1.2.3.4.3.2.3.4.6.7.6];
let unique = (arr) = > {
    let result = [];
    for (var i = 0; i < arr.length; i++) {
        if (result.indexOf(arr[i]) === -1) { result.push(arr[i]); }}return result;
}
var result = unique(arr);
console.log(result);   //[1, 2, 3, 4, 6, 7]
Copy the code
  1. indexOf() + filter
let arr = [1.2.3.4.3.2.3.4.6.7.6];
let unique = (arr) = > {
    return arr.filter((item, index) = > {
        returnarr.indexOf(item) === index; })}var result = unique(arr);
console.log(result);     //[1, 2, 3, 4, 6, 7]
Copy the code
  1. Object key value pairs are de-duplicated. Store the value of an array as the key of an Object, for exampleObject[value1] = trueIf, when judging another value, ifObject[value2]If it exists, the value is repeated.
function unique(arr) {
    let hashTable = {};
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
        if(! hashTable[arr[i]]) { hashTable[arr[i]] =true; newArr.push(arr[i]); }}return newArr;
}
let arr = [1.2.3.4.3.2.3.4.6.7.6];
let result = unique(arr);
console.log(result);     //[1, 2, 3, 4, 6, 7]
Copy the code