Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一些巧妙的方法 #9

Open
WangXiZhu opened this issue Oct 11, 2016 · 1 comment
Open

一些巧妙的方法 #9

WangXiZhu opened this issue Oct 11, 2016 · 1 comment

Comments

@WangXiZhu
Copy link
Owner

  • 数组[1,2,3]转为对象{'1':1,'2':2,'3':3}的形式
    1.方式1-es6简单粗暴
var arr = [1,2,3];
var obj = Object.assign({},arr); 
console.info(obj);   //{0: 1, 1: 2, 2: 3}

2.方式2

var obj = {};
var arr = [1,2,3];
arr.unshift.apply(obj,arr);
console.info(obj);   //{0: 1, 1: 2, 2: 3, length: 3}

其中比较直观的区别是方式2中的对象会多一个length属性

在时间上如果比较效率的话有图为证

在不考虑兼容性的情况下,方式1跟好一点
兼容性
在移动端目前支持不太好。

@WangXiZhu
Copy link
Owner Author

  • 类数组转为数组
var arr = [].slice.call(document.querySelectorAll('.controls input'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant