本两周学习总结

去重

const unique = (arr) => arr.reduce(
    (acc, val) => (acc.indexOf(val) == -1 ? acc.push(val) : acc, acc), []);

const unique = (arr) => arr.filter((val,index)=>arr.indexOf(val)==index);

let arr = [1, 2, 1, 1, 2, 3, 3, 4, 1, 1, 2, 2, 5];
console.log(unique(arr));//[ 1, 2, 3, 4, 5 ]

iterator 迭代器

Iterator 接口主要为for of 服务的,供for...of 进行消费

首先他作为一个对象,这个对象有一个next 方法,每次调用next 方法都会返回一个结果值

这个结果值是一个object ,包含两个属性,valuedone

value 表示具体的返回值,done是布尔类型的,表示集合是否遍历完成或者是否后续还有可用数据,有false,没有true

代码实现

const getIterator = list => {
  let i = 0;
  return {
    next: () => ({
      done: (i >= list.length),
      value: list[i++]
    })
  }
};
let a = getIterator([1, 2, 3]);
console.log(a.next()); //{ done: false, value: 1 }
console.log(a.next()); //{ done: false, value: 2 }
console.log(a.next()); //{ done: false, value: 3 }
console.log(a.next()); //{ done: true, value: undefined }

给对象添加Symbol.iterator 属性,此属性指向一个迭代器方法,这个迭代器会返回一个特殊的对象-迭代器对象

var arr=[100,200,300];

var iteratorObj=  arr[Symbol.iterator]();//得到迭代器方法,返回迭代器对象
console.log(iteratorObj.next());

判断对象是否是可迭代的

既然可迭代对象的规则必须在对象上部署Symbol.iterator 属性,可以判断对象是否为可迭代对象,

const isIterable = obj => typeof obj[Symbol.iterator] === 'function';

解构

数组的元素是按次序排列的,变量的取值由位置决定的;

对象的属性没有次序,变量必须与属性同名,才能取到正确的值

let [a, b, c] = [1, 2, 3];
console.log(a, b, c);//1,2,3
let {age,name}={name:'zs', age: 12};
console.log(name, age);// 'zs'  12

对象的解构

let {random}=Math;
console.log(random() * 10);

前端大面试

http://bigerfe.com/

trimStart trimEnd

trimStart 只清除头部的空格

trimEnd 只清楚尾部的空格

去重

const responseList = [
  { id: 1, a: 1 },
  { id: 2, a: 2 },
  { id: 3, a: 3 },
  { id: 1, a: 4 },
];
const result = responseList.reduce((acc, cur) => {
    const ids = acc.map(item => item.id);
    return ids.includes(cur.id) ? acc : [...acc, cur];
}, []);

发现一个有趣的需求

const capitalizeHeadline = str => {
  return str.split(' ').reduce((acc, val, index, array) => {
    return acc + ((index == 0 || index == array.length - 1) ? val[0].toUpperCase() + val.substr(1) : val)+' ';
  }, '')
};
console.log(capitalizeHeadline('abd sslsl dddd'));
// Abd sslsl Dddd 

给数字添加千分位逗号

const transform = (num = 0) => {
 if (num < 0) {
    return num
  }
  let numStr = num + '';
  let arr = numStr.split('.');
  let a = arr[0].split('');
  for (let i = a.length - 3; i > 0; i = i - 3) {
    a[i] = ',' + a[i];
  }
  return arr[1] ? a.join('') + '.' + arr[1] : a.join('')
};

CSS灵感

https://chokcoco.github.io/CSS-Inspiration/#/./shadow/one-sided-shadow.md

leetCode121 买卖股票的最佳时机

const solution = nums => {
  if (nums.length < 2) {
    return 0
  }
  //最大值得初始化为0,最小值为第一个数,循环从1开始
  let max = 0, min = nums[0], i = 0;
  while (++i < nums.length) {
    min = Math.min(min, nums[i]);
    max = Math.max(max, nums[i] - min)
  }
  return max
};

从排序数组中删去重复的项

const removeDuplicates = nums => {
  if (nums.length < 2) {
    return 0
  }
  let i = 0;
  for (let j = 1; j < nums.length; j++) {
    if (nums[i] != nums[j]) {
      ++i;
      nums[i] = nums[j]
    }
  }
  nums.length = i + 1;
  return nums
};

const removeDuplicates = nums => nums.reduce((acc, val) =>
    acc.indexOf(val) === -1 ? acc.concat(val) : acc
, []);

反转字符串中的单词

const reverseWord = str => str.split(' ')
  .reduce((acc, val) =>acc.concat( val.split('').reverse().join('') + ' ')
  , '');
console.log(reverseWord('abc cdfg smd'));

905. 按奇偶排序数组

const sortArray = arr => {
  let a = 0, max = arr.length - 1, res = [];
  for (let item of arr) {
    if (item % 2 == 0) {
      res[a++] = item
    } else {
      res[max--] = item
    }
  }
  return res
};
console.log(sortArray([1, 2, 3, 4, 5, 6]));
// [ 2, 4, 6, 5, 3, 1 ]

复习 && || !

!   取反
a||b    a为真返回a   a为假返回b
a&&b    a为真返回b   a为假返回a

判断字符串或者数组是否唯一

const inUnique = str => {
  let obj = {};
  for (let item of str) {
    if (obj[item]) {
      return false
    }
    obj[item] = true
  }
  return true
};

改变跑马灯的时间

$('.marquee-box').css('animation-duration','10s')

数组转对象

Object.assign({}, [1, 2, 3, 4]);
// { '0': 1, '1': 2, '2': 3, '3': 4 }

使得两个数之和等于目标值

const twoSum = (array, target) => {
  for (let i = 0; i < array.length; i++) {
    let a = array.indexOf(target - array[i]);
    if (a > -1 && a != i) {
      return [i, a]
    }
  }
  return [-1, -1]
};
console.log(twoSum([1, 2, 3, 4, 5, 6], 3));
原文地址:https://www.cnblogs.com/fangdongdemao/p/11982966.html