30s就能懂的js代码

arrayMax (返回数组中的最大数)

const arrayMax = arr => Math.max(...arr);
// arrayMax([10, 1, 5]) -> 10

difference(比较两个数组的不同)

const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); };
// difference([1,2,3], [1,2,4]) -> [3]

distinctValuesOfArray (返回去重后的数组内容)

const distinctValuesOfArray = arr => [...new Set(arr)];
// distinctValuesOfArray([1,2,2,3,4,4,5]) -> [1,2,3,4,5]

shuffle(打乱一个数组)

const shuffle = arr => arr.sort(() => Math.random() - 0.5);
// shuffle([1,2,3]) -> [2,3,1]

getURLParameters(返回一个包含 URL 中参数的对象)

const getURLParameters = url =>
  url.match(/([^?=&]+)(=([^&]*))/g).reduce(
    (a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {}
  );
// getURLParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}

getDaysDiffBetweenDates(返回两个日期间相差的天数)

const getDaysDiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateInitial) / (1000 * 3600 * 24);
// getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) -> 9

你可能感兴趣的:(30s就能懂的js代码)