一、构造数组

1 - Array

  • 创建一个数组:new Array(item1,item2……)
  • 字面量创建一个数组:let array = []

    2 - 转换数组

  • Array.of

  • Array.from

    1. let arr3 = Array.of("a","b");
    2. let arr4 = Array.of(3); // 把传进来的值作为数组的第一项
    3. let arr5 = Array.from({0: 'a', 1: 'b', 2:'c', length: 3});
    4. console.log(arr3) // [ 'a', 'b' ]
    5. console.log(arr4) // [ 3 ]
    6. console.log(arr5) // [ 'a', 'b', 'c' ]

    二、判断数组

    五种判断数组的方式:

  • API判断 - Array.isArray

  • instanceof 判断
  • 原型判断
    • Array.prototype.isPrototypeOf
    • Object.getPrototypeOf
    • Object.prototype.toString ```javascript let newArray = new Array(“a”); // 以下返回结果都为true let array1 = [1]; console.log(Array.isArray(newArray)); console.log(Array.isArray(array1));

console.log(newArray instanceof Array); console.log(array1 instanceof Array);

console.log(newArray.constructor === Array) console.log(array1.constructor === Array)

console.log(Array.prototype.isPrototypeOf(newArray)); Array.prototype.isPrototypeOf(array1);

console.log(Object.getPrototypeOf(newArray) === Array.prototype); console.log(Object.getPrototypeOf(array1) === Array.prototype);

console.log(Object.prototype.toString.call(newArray) === ‘[object Array]’); console.log(Object.prototype.toString.call(array1) === ‘[object Array]’);

  1. <a name="Jp9QS"></a>
  2. ## 三、数组的API
  3. <a name="KFeLM"></a>
  4. ### 1 - 会改变数组本身(9个)
  5. 主要为:增删改
  6. - push/pop (新增/删除)
  7. - unshift/shift (新增/删除)
  8. - reverse (反转)
  9. - sort (排序)
  10. - splice (截取)
  11. - copyWithin (复制)
  12. - fill (填充)
  13. ```javascript
  14. let originArray = ["Tom","July","Lily","VC"];
  15. let pushData = originArray.push("PUSH");
  16. console.log(pushData) // 5
  17. console.log(originArray); // ["Tom","July","Lily","VC","PUSH"]
  18. let popData = originArray.pop();
  19. console.log(popData); // PUSH
  20. console.log(originArray); //["Tom","July","Lily","VC"]
  21. let unshiftData = originArray.unshift("UNSHIFT");
  22. console.log(unshiftData); // 5
  23. console.log(originArray); // ["UNSHIFT","Tom","July","Lily","VC"]
  24. let shiftData = originArray.shift();
  25. console.log(shiftData); // UNSHIFT
  26. console.log(originArray); //["Tom","July","Lily","VC"]
  27. let reverseData = originArray.reverse()
  28. console.log(reverseData); // [ 'VC', 'Lily', 'July', 'Tom' ]
  29. console.log(originArray); // [ 'VC', 'Lily', 'July', 'Tom' ]
  30. let sortData = originArray.sort((a,b) => {return a-b});
  31. console.log(sortData); // [ 'VC', 'Lily', 'July', 'Tom' ]
  32. console.log(originArray); // [ 'VC', 'Lily', 'July', 'Tom' ]
  33. let spliceData = originArray.splice(0,3);
  34. console.log(spliceData); // [ 'VC', 'Lily', 'July' ]
  35. console.log(originArray);// [ 'Tom' ]
  36. let copyWithin = originArray.copyWithin(0,3);
  37. console.log(copyWithin);
  38. console.log(originArray);
  39. let fillData = originArray.fill(2,3,5);
  40. console.log(fillData);
  41. console.log(originArray);

2 - 不会改变数组本身(9个)

主要为:拼接、查找、转换

  • concat (拼接数组)
  • join (拼接数组)
  • slice (截取数组)
  • indexOf (从前向后查找数组)
  • lastIndexOf(从后向前查找数组)
  • includes (ES6新增 查找数组)
  • toString (数组转换为字符串)
  • toLocaleString(数组转换为字符串)
  • flat(拉平数组) ```javascript // concat/join/slice/toString/toLocaleString/indexOf/lastIndexOf/includes/flat let originArr1 = [1,2,3]; let originArr2 = [3,5,4,5,6]; let concatData = originArr1.concat(…originArr2); console.log(concatData); // [ 1, 2, 3, 3, 5, 4, 5, 6 ]

let joinData = originArr2.join(“&”); console.log(joinData); // 3&5&4&5&6

let sliceData = originArr2.slice(1,4); console.log(sliceData); // [ 5, 4, 5 ]

let toString = originArr2.toString(); console.log(toString); // 3,5,4,5,6

let toLocaleString = originArr2.toLocaleString(); console.log(toLocaleString); // 3,5,4,5,6

let indexData = originArr2.indexOf(5); console.log(indexData); // 1

let lastIndexData = originArr2.lastIndexOf(5); console.log(lastIndexData); // 3

  1. <a name="DSxW5"></a>
  2. ### 3 - 遍历数组(13个)
  3. ES6新增 keys、values、entries、find、findIndex、flatMap
  4. - forEach
  5. - map
  6. - some
  7. - every
  8. - filter
  9. - reduce
  10. - reduceRight
  11. - keys
  12. - values
  13. - entries
  14. - find
  15. - findIndex
  16. - flatMap
  17. <a name="BJPbj"></a>
  18. ## 四、常见的排序算法
  19. <a name="GkTgD"></a>
  20. ### 1 - 冒泡排序
  21. > 冒泡排序是比较两个相邻的元素,将值大的元素交换到右边
  22. ```javascript
  23. function bubbleSort(array) {
  24. let length = array.length;
  25. if (length < 2) return array;
  26. // 第一个循环控制要比较的趟数
  27. for (let i = 0; i < length; i++) {
  28. // 第二个循环是当前趟要比较的个数
  29. for (let j = 0; j < i; j++) {
  30. if (array[j] > array[i]) {
  31. let temp = array[j];
  32. array[j] = array[i];
  33. array[i] = temp;
  34. }
  35. }
  36. }
  37. return array;
  38. }

2 - 快速排序

选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分;其中一部分的所有数据都比另外一部分的所有数据都要小。然后,再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

  1. function quickSort(array) {
  2. let quick = function (arr) {
  3. if (arr.length <= 1) return arr;
  4. let index = Math.floor(arr.length / 2);
  5. let pivot = arr.splice(index, 1)[0];
  6. let left = [], right = [];
  7. for (let i = 0; i < arr.length; i++) {
  8. if (arr[i] < pivot) {
  9. left.push(arr[i])
  10. } else {
  11. right.push(arr[i])
  12. }
  13. }
  14. return quick(left).concat([pivot], quick(right))
  15. }
  16. return quick(array);
  17. }

3 - 插入排序

从数组的第二个数据开始往前比较,即一开始用第二个数和他前面的一个比较,如果符合条件则插入;然后在用第三个和第二个比较,符合条件插入,但是还得继续网签比较,直到不符合条件

  1. function insertSort(array) {
  2. let len = array.length;
  3. let current, prev;
  4. for (let i = 1; i < len; i++) {
  5. current = array[i];
  6. prev = i - 1;
  7. while (prev >= 0 && array[prev] > current) {
  8. array[prev + 1] = array[prev];
  9. array[prev] = current
  10. prev--
  11. }
  12. }
  13. return array;
  14. }

4 - 选择排序

每次循环获取到剩余位置的最小值的索引,交换位置

  1. function selectSort(array) {
  2. let len = array.length;
  3. let minIndex;
  4. for (let i = 0; i < len; i++) {
  5. minIndex = i;
  6. for (let j = i; j < len; j++) {
  7. if (array[j] < array[i]) {
  8. minIndex = j
  9. }
  10. }
  11. let temp = array[minIndex];
  12. array[minIndex] = array[i];
  13. array[i] = temp;
  14. }
  15. return array;
  16. }

5 - 归并排序

先把数组拆分成一定个数的有序小数组,然后把小数组合并

  1. function mergeSort(array) {
  2. function merge(left, right) {
  3. const result = [];
  4. let leftIndex = 0;
  5. let rightIndex = 0;
  6. while (leftIndex < left.length && rightIndex < right.length) {
  7. if (left[leftIndex] < right[rightIndex]) {
  8. result.push(left[leftIndex++])
  9. } else {
  10. result.push(right[rightIndex++])
  11. }
  12. }
  13. while (leftIndex < left.length) {
  14. result.push(left[leftIndex++])
  15. }
  16. while (rightIndex < right.length) {
  17. result.push(right[rightIndex++])
  18. }
  19. return result
  20. }
  21. const mergeSortInner = (array) => {
  22. if (array.length === 1) return array;
  23. let mid = Math.floor(array.length / 2);
  24. let left = array.slice(0, mid);
  25. let right = array.slice(mid, array.length);
  26. return merge(mergeSortInner(left), mergeSortInner(right))
  27. }
  28. return mergeSortInner(array)
  29. }