首先
对于一个前端来说, 尤其是笔试面试的时候,算法方面其实考的不难,但是对于一个初学JavaScript或者对于不知道如何快速写算法的你来说,可能是很容易混淆的。
所以, 小编这次带来了Tips / Tricks ,让你轻松面对!
8 Tips
在ECMAScript 6.0 (简称 ES6)是JavaScript 语言的下一代标准。
目的是使JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言。
PS: 以下所有代码都尊重 immutability principles (不变形原则), 从不改变 initial object (最初对象), 而是返回 a new object that have the desired properties (所需属性的新对象)。
1. Uniq Array
Create Set from our values, and then convert it back to an array ( if need be):
在转换回array之前, 记住Set自己也有一些有用的工具,例如size或者has
2. Update an array object by property
Update that has id: 3in the array.
当然, 这里的‘initial’ const 没有动。
3. Remove an object from array by property
Remove the object that has id === 3 from our array:
又一次, initial 没有改变。
4. Remove a key from an object
Denormalized(非规格化) assignment the other-way around:
5. Merge an array of objects together
这里,我们可以merge objects together, 或者 update them by their properties:
reduce 和 spread operators(扩展预算符)搭配的很好!
6. Flatten
One-liner to flatten an array:
7. From Pairs
One-liner: create object from pairs:
[key, value] denormalized assignment a lot
8. Subtract two Sets
不是很经常用,但是还是很有帮助的。
最后
希望大家都渐渐掌握这些小技巧, 慢慢的成为JavaScript大神,帮助大家在以后的求职的道路上又knock down一只拦路虎。