码迷,mamicode.com
首页 > 其他好文 > 详细

本周学习总结

时间:2019-12-24 11:49:17      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:symbol   名称   学习   art   视频播放   for循环   驼峰命名   播放   chinese   

数组扁平化

let arr = [1, 2, 3, 1, [1, 2, [1, 2, 3, 4, [1, 2, 3]], 3, [2, 3, 4, 5]]];

while (arr.some(val => Array.isArray(val))) {
  arr=[].concat(...arr)
}

把数字转换成中文

const toChineseNum = num => {
  num += '';
  let len = num.length;
  let numStr = '零一二三四五六七八九十';
  let unitArr = ['', '十', '百', '千', '万'];
  let cha, unit;
  const getResult = str => {
    if (str.length > 5) {
      cha = str.length - 5;
      unit = unitArr.slice(1, cha + 1).reverse();
    }
    let res = Array.from({length: str.length}).reduce((acc, val, i) => {
      //大于5位的前面部分
      if (str.length > 5) {
        return acc.concat(numStr[str[i]] + (i < cha ? unit[i] : unitArr[str.length - 1 - i]));
      } else {
        return acc.concat(numStr[str[i]] + unitArr[str.length - 1 - i]);
      }
    }, '');
    return res.replace(/零[零十百千]+/g, '零').replace(/零+$/g, '').replace(/零万/g, '万')
  };
  if (len > 8) {
    return getResult(num.slice(0, len - 8)) + '亿' + getResult(num.slice(-8))
  }
  return getResult(num)
};

转换驼峰命名

const toCamelCase=str=>str.replace(/[_\-](\w)/g,(all,val)=>{
    return val.toUpperCase()
});
console.log(toCamelCase('new_dkg-kdk'));

合并两个有序的数组

const merge = (num1, num2, n = num1.length, m = num2.length) => {
  let len1 = n - 1;
  let len2 = m - 1;
  let len = n + m - 1;
  while (len1 >= 0 && len2 >= 0) {
    num1[len--] = num1[len1] > num2[len2] ? num1[len1--] : num2[len2--];
  }
  return num1;
};

分糖果(最大糖果的种类数)

const distribut = arr => Math.min(new Set(arr).size, arr.length >> 1);

递归=>动态规划

// 递归
const sum = n => {
  if (n == 1) {
    return 1
  }
  if (n == 2) {
    return 2
  }
  return sum(n - 1) + sum(n - 2)
};
// 动态规划
const climb = n => {
  const dp = [];
  dp[0] = 1;
  dp[1] = 2;
  for (let i = 2; i < n; i++) {
    dp[i] = dp[i - 1] + dp[i - 2]
  }
  return dp[n - 1]
};
console.log(climb(10));

experss 打开mysql

https://github.com/tgxhx/node-book-api

mysql: {
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'airchat',
    port: 3306,
  },

视频播放网址

https://github.com/xiugangzhang/video.github.io
先导入video.sql,test.sql
然后修改mysql的地址,就行了

Symbol.unscopables

用于指定对象值,其对象自身和继承的从关联对象的with环境绑定中排除的属性名称

const object1 = {
  property1: 42
};

object1[Symbol.unscopables] = {
  property1: true
};

with (object1) {
  console.log(property1);
  // expected output: Error: property1 is not defined
}
Symbol.unscopables 属性的属性特性:
writable false
enumerable false
configurable false

Array.prototype[Symbol.unscopables]

所有 ES2015 (ES6) 中新定义的、且并未被更早的 ECMAScript 标准收纳的属性名。这些属性被排除在由 with 语句绑定的环境中。

with 绑定中未包含的数组默认属性有:

重写fill方法

转成数字
const toInteger = x => isNaN(x = +x) ? 0 : x > 0 ? Math.floor(x) : Math.ceil(x);
当初始化的值为负数,大于length,或者小数
const toAbsolute = (index, len) => {
  let x = toInteger(index);
  return x < 0 ? Math.max(x + len, 0) : Math.min(x + len, len);
};

function fills(array, value, start, end) {
  let index = toAbsolute(start,array.length);
  let ends = end === undefined ? array.length : toInteger(end,array.length);
  console.log(index, ends);
  while (ends > index) array[index++] = value;
  return array
}

let arr = [1, 2, 3, 4, 5, 6];
console.log(fills([1, 2, 3, 4], 2,-1));

for循环的另一种写法

let i=10;
for (;i < 13; i++) {
  console.log(i);
}

一个函数实现map,filter,forEach,some,every,find,findIndex

const createMethod = type => {
  let is_map = type == 1,
    is_filter = type == 2,
    is_some = type == 3,
    is_every = type == 4,
    is_find_index = type == 6,
    no_holes = type == 5 || is_find_index;
  return (array, fn) => {
    let value, result;
    let length = array.length;
    let create = (len) => Array.from({length: len});
    let target = is_map ? create(length) : is_filter ? create(0) : undefined;
    for (let i = 0; i < array.length; i++) {
      if (no_holes || i in array) {
        value = array[i];
        result = fn(value, i, array);
        if (type) {
          if (is_map) {
            target[i] = result;
          } else if (result) {
            switch (type) {
              case 3:
                return true;// some
              case 5:
                return value;//find
              case 6:
                return i;//findIndex
              case 2:
                target.push(value);//filter
            }
          } else if (is_every) {
            return false;//every
          }
        }
      }
    }
    return is_find_index ? -1 : is_some || is_every ? is_every : target;
  }
};
function iterations() {
  return ({
    forEach: createMethod(0),
    map: createMethod(1),
    filter: createMethod(2),
    some: createMethod(3),
    every: createMethod(4),
    find: createMethod(5),
    findIndex: createMethod(6)
  })
}

let map=iterations().map;
// console.log(map([1, 2, 3, 4], val => val * 2));
let filter=iterations().filter;
// console.log(filter([1, 2, 3, 4, 5], (val, i) => i < 3));
let some=iterations().some;
// console.log(some([1, 2, 3, 4, 5], val => val > 3));
let every=iterations().every;
// console.log(every([1, 2, 3, 4, 5], val => val > 2));//false
// console.log(every([1, 2, 3, 4, 5], val => val > 0));// true
let find=iterations().find;
console.log(find([1, 2, 3, 4, 5], val => val > 3));// 4
let findIndex=iterations().findIndex;
// console.log(findIndex([1, 2, 3, 4, 5], val => val > 4));
let forEach=iterations().forEach;
let arr = [1, 2, 3, 4, 5, 6];
console.log(forEach(arr, (val,i) => arr[i]=1));// undefined
console.log(arr);// [1,1,1,1,1,1]

本周学习总结

标签:symbol   名称   学习   art   视频播放   for循环   驼峰命名   播放   chinese   

原文地址:https://www.cnblogs.com/fangdongdemao/p/12090391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!