数组扁平化(又称数组降维)

Array.flat()

  1. const test = ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]]
  2. // flat不传参数时,默认扁平化一层
  3. test.flat()
  4. // ["a", "b", "c", "d", ["e", ["f"]], "g"]
  5. // flat传入一个整数参数,整数即扁平化的层数
  6. test.flat(2)
  7. // ["a", "b", "c", "d", "e", ["f"], "g"]
  8. // Infinity 关键字作为参数时,无论多少层嵌套,都会转为一维数组
  9. test.flat(Infinity)
  10. // ["a", "b", "c", "d", "e", "f", "g"]
  11. // 传入 <=0 的整数将返回原数组,不扁平化
  12. test.flat(0)
  13. test.flat(-1)
  14. // ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]]
  15. // 如果原数组有空位,flat()方法会跳过空位。
  16. ["a", "b", "c", "d",,].flat()
  17. // ["a", "b", "c", "d"]

使用reduce方法

  1. function flat(arr, depth = 1) {
  2. return depth > 0
  3. ? arr.reduce((acc, cur) => {
  4. if(Array.isArray(cur)) {
  5. return [...acc, ...flat(cur, depth-1)]
  6. }
  7. return [...acc, cur]
  8. } , [])
  9. : arr
  10. }
  11. // 测试
  12. var test = ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]]
  13. // 不传参数时,默认扁平化一层
  14. flat(test)
  15. // ["a", "b", "c", "d", ["e", ["f"]], "g"]
  16. // 传入一个整数参数,整数即扁平化的层数
  17. flat(test, 2)
  18. // ["a", "b", "c", "d", "e", ["f"], "g"]
  19. // Infinity 关键字作为参数时,无论多少层嵌套,都会转为一维数组
  20. flat(test, Infinity)
  21. // ["a", "b", "c", "d", "e", "f", "g"]
  22. // 传入 <=0 的整数将返回原数组,不扁平化
  23. flat(test, 0)
  24. flat(test, -10)
  25. // ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]];
  26. // 如果原数组有空位,flat()方法会跳过空位。
  27. var arr = ["a", "b", "c", "d",,]
  28. flat(arr)
  29. // ["a", "b", "c", "d"]

  1. function flat(arr, depth = 1) {
  2. return depth > 0
  3. ? arr.reduce((acc, cur) => {
  4. if(Array.isArray(cur)) {
  5. return [...acc, ...flat(cur, depth-1)]
  6. }
  7. return [...acc, cur]
  8. } , [])
  9. : arr
  10. }
  11. // 测试
  12. var test = ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]]
  13. // 不传参数时,默认扁平化一层
  14. flat(test)
  15. // ["a", "b", "c", "d", ["e", ["f"]], "g"]
  16. // 传入一个整数参数,整数即扁平化的层数
  17. flat(test, 2)
  18. // ["a", "b", "c", "d", "e", ["f"], "g"]
  19. // Infinity 关键字作为参数时,无论多少层嵌套,都会转为一维数组
  20. flat(test, Infinity)
  21. // ["a", "b", "c", "d", "e", "f", "g"]
  22. // 传入 <=0 的整数将返回原数组,不扁平化
  23. flat(test, 0)
  24. flat(test, -10)
  25. // ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]];
  26. // 如果原数组有空位,flat()方法会跳过空位。
  27. var arr = ["a", "b", "c", "d",,]
  28. flat(arr)
  29. // ["a", "b", "c", "d"]

数组去重

set (ES6)

  1. function unique(arr) {
  2. return Array.from(new Set(arr))
  3. }
  4. // 或者
  5. var unique = arr => [...new Set(arr)]

reduce

  1. function unique (arr) {
  2. return arr.sort().reduce((acc, cur) => {
  3. if (acc.length === 0 || acc[acc.length - 1] !== cur) {
  4. acc.push(cur);
  5. }
  6. return acc
  7. }, [])}
  8. ;

filter

  1. function unique(arr) {
  2. return arr.filter( (element, index, array) => {
  3. return array.indexOf(element) === index
  4. })
  5. }

去掉字符串中的空格

  1. trim(str){
  2. if (!str) {
  3. return '';
  4. }
  5. return str.replace(/\s*/g,'');
  6. }

判断两个对象是否相同

  1. isObjectValueEqual(x, y) {
  2. // 指向同一内存时
  3. if (x === y) {
  4. return true;
  5. } else if (
  6. typeof x == 'object' &&
  7. x != null &&
  8. typeof y == 'object' && y != null
  9. ) {
  10. if (Object.keys(x).length != Object.keys(y).length) return false;
  11. for (var prop in x) {
  12. if (y.hasOwnProperty(prop)) {
  13. if (!this.isObjectValueEqual(x[prop], y[prop])) return false;
  14. } else return false;
  15. }
  16. return true;
  17. } else return false;
  18. }