1. 数组排序

1.1 冒泡排序

  1. //将第一个值与身后相邻的值进行比较,若身后相邻的值小则与第一个值交换位置。
  2. const arr = [11, 33, -5, 17, 3, 59, 259, -39, 152];
  3. for (var i = 0; i <= arr.length - 1; i++) {
  4. for (var j = 0; j <= arr.length - i - 1; j++) {
  5. if (arr[j] > arr[j + 1]) {
  6. var temp = arr[j]
  7. arr[j] = arr[j + 1]
  8. arr[j + 1] = temp
  9. }
  10. }
  11. }
  12. console.log(arr)

2. 选择排序

  • 首先在未排序序列中找到最小的元素,存放在排序序列的起始位置。
  • 再从剩余未排序元素中继续寻找最小元素,然后放在已经排序列的末尾
  • 重复以上步骤.
    1. var arr = [10, 90, 55, 96, 23, 8, 79];
    2. for (var i = 0; i < arr.length - 1; i++) {
    3. var minIndex = i;
    4. for (var j = i + 1; j < arr.length; j++) {
    5. if (arr[j] > arr[minIndex]) {
    6. minIndex = j
    7. }
    8. }
    9. const temp = arr[i];
    10. arr[i] = arr[minIndex]
    11. arr[minIndex] = temp
    12. }

    1.2 sort排序

    1. const arr = [11, 33, -5, 17, 3, 59, 259, -39, 152];
    2. arr.sort((a, b) => {
    3. return a - b
    4. })
    5. console.log('2', arr)

    2. 数组去重

    ```javascript //第一种 new Set() const arr1 = [11, 22, 33, 14, 22, 33] const arr2 = […new Set(arr1)] console.log(arr2) //[11, 22, 33, 14]

//第二种 indexOf const arr1 = [11, ‘a’, 22, 33, 14, 22, 33, ‘a’, null] let arr2 = [] arr1.forEach(item => { //如果检索的字符串值没有出现, 则返回-1 if (arr2.indexOf(item) == -1) { arr2.push(item) } }) console.log(arr2) //[11, ‘a’, 22, 33, 14, null]

//第三种 includes const arr1 = [11, ‘a’, 22, 33, 14, 22, 33, ‘a’, null] let arr2 = [] arr1.forEach(item => { //如果检索的字符串值没有出现, 则返回-1 if (!arr2.includes(item)) { arr2.push(item) } }) console.log(arr2) //[11, ‘a’, 22, 33, 14, null]

//第四种 reduce + includes const arr1 = [11, ‘a’, 22, 33, 14, 22, 33, ‘a’, null,33] let arr2 = arr1.reduce((prev, cur) => prev.includes(cur) ? prev : […prev, cur], [])

console.log(arr2) //[11, ‘a’, 22, 33, 14, null]

//第五种 reduce + indexOf const arr1 = [11, ‘a’, 22, 33, 14, 22, 33, ‘a’, null] let arr2 = arr1.reduce((prev, cur) => prev.indexOf(cur) !== -1 ? prev : […prev, cur], [])

console.log(arr2) //[11, ‘a’, 22, 33, 14, null]

//第六种 filter + indexOf const arr1 = [11, ‘a’, 22, 33, 14, 22, 11, ‘a1’, null] let arr2 = arr1.filter((item, index, arr) => { //当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素 return arr.indexOf(item, 0) === index })

console.log(arr2) //[11, ‘a’, 22, 33, 14, ‘a1’,null]

  1. ```javascript
  2. var arr = [
  3. {
  4. key: '01',
  5. value: '乐乐',
  6. },
  7. {
  8. key: '02',
  9. value: '博博',
  10. },
  11. {
  12. key: '03',
  13. value: '淘淘',
  14. },
  15. {
  16. key: '04',
  17. value: '哈哈',
  18. },
  19. {
  20. key: '01',
  21. value: '乐乐',
  22. },
  23. ]
  24. //第一种方法
  25. let obj = {};
  26. for (var i = 0; i < arr.length; i++) {
  27. obj[arr[i]['key']] = arr[i]
  28. }
  29. console.log(Object.values(obj))
  30. //第二种方式
  31. let obj = {};
  32. let resData = []
  33. arr.forEach(item => {
  34. if (!obj[item['key']]) {
  35. resData.push(item)
  36. obj[item['key']] = true
  37. }
  38. })
  39. //第三种
  40. const obj = new Map();
  41. const resData = arr.filter(item => !obj.has(item['key']) && obj.set(item['key'], 1))
  42. console.log(resData)
  43. //第四种
  44. let resData = []
  45. arr.forEach((item, index) => {
  46. //findIndex作用:找到遍历的数组中的第一个符合判断条件的值,并返回该值对应的索引,停止遍历
  47. const findIndex = arr.findIndex(each => each.key === item.key)
  48. if (findIndex === index) {
  49. resData.push(item)
  50. }
  51. })