The sorting

ascending

function bubblArrSort(arr) { var len = arr.length, d; for (var i = 0; i < len; i ++) { for (var j = i; j < len; J ++) {if (arr[I] > arr[j]) {d = arr[j] arr[I] = arr[I] arr[I] = d}} return arr} or/ or arr.sort()Copy the code

Descending order

function bubblArrDescend(arr) {
  var len = arr.length, d;
  for (var i = 0; i < len; i ++) {
    for (var j = i; j < len; j ++) {
      if (arr[i] < arr[j]) {
        d = arr[j]
        arr[j] = arr[i]
        arr[i] = d
      }
    }
  }
  return arr
}

or

arr.sort().reverse()
Copy the code

duplicate removal

function uniqueArr(arr) { var newArr = [] for (var i = 0; i < arr.length; i ++) { if (newArr.indexOf(arr[i]) === -1) { newArr.push(arr[i]) } } return newArr } or new Set(arr) or arr.filter(function(v,i,self){ return self.indexOf(v) == i; }) or arr.reduce(function (prev, cur) { prev.indexOf(cur) === -1 && prev.push(cur); return prev; }, [])Copy the code

Every () and some ()

Every () checks whether every item in the array satisfies the condition, and returns true only when all items satisfy the condition

Some () checks if there are any items in the array that satisfy the condition, and returns true as long as one item satisfies the condition

Reduce () and reduceRight ()

Reduce () takes two arguments: the function and the initial value of the recursion. Start with the first item of the array and walk through to the end reduceRight() starting with the last item of the array and walking forward to the first item

arr.reduce(function(prev,cur,index,arr){ ... }, init); Arr stands for primitive array; Prev represents the return value from the last call to the callback, or the initial value init; Cur represents the array element currently being processed; Index represents the index of the array element being processed, 0 if init is provided, 1 otherwise; Init represents the initial value.Copy the code

Array to object

arr.reduce((prev, cur) => {prev[cur.id] = cur; return prev; }, {})Copy the code

Object array deduplicating (juejin.cn/post/695416…)

Reduce flattened array into tree data (from she_will) www.cnblogs.com/shewill/p/1…