An array of methods to re-summary

unique1 = function () {let newArr = [... new Set (this)]; return newArr;} let arr1 = arr.unique1 (); console.log (arr); console.log (arr1); * / Method four / * let arr = [1, 1, 3, 3, 2, 2, 5, 5, 6, 6, 7, 7]; let nowArr = [] Array.prototype.unique2 = function () {let newArr = [this [0]]; // a new temporary array for (let i = 1; i <this.length; i ++) {// the second traversing if (newArr.indexOf (this [i]) = = -1) {// indexOf: returns the value of a specified position of the first occurrence in the array (string) of. newArr.push (this [i]); // Return Value: -1 indicating that the array number did not appear in the temporary array to push go} // returns the value -1 (for the index value in the array! ) described in this temporary array value already exists ignore} return newArr;} let arr1 = arr.unique2 (); console.log (arr); console.log (arr1); * / // five methods to iterate the optimization method / / var arr = [1, 3, 4, 5, 3, 4, 8, 9, 8, 4]; // for (var i = 0; i <arr.length; i ++) {// for (var j = i + 1; j <arr.length; j ++) {// if (arr [i] == arr [j]) {// arr.splice (j, 1); // j--; //} //} //} // console.log (arr) // [1,3,4,5,8,

Guess you like

Origin www.cnblogs.com/hhmmpp/p/11070254.html