Skip to content

Instantly share code, notes, and snippets.

@vino24
Last active June 1, 2017 15:56
Show Gist options
  • Save vino24/72dbe64697005aac87325df9c988daeb to your computer and use it in GitHub Desktop.
Save vino24/72dbe64697005aac87325df9c988daeb to your computer and use it in GitHub Desktop.
类数组转数组
const arrLike = {
length: 4,
2: "foo"
};
Array.from(arrLike); // [undefined, undefined, "foo", undefined] , length 是类数组对象必须的属性
// Array.from还可以接受一个映射函数作为第二个参数类似Array.map(..)
var arrLike = {
length: 4,
2: "foo"
};
Array.from(arrLike, function mapper(val, idx){
if (typeof val == "string") {
return val.toUpperCase();
}
else {
return idx;
}
} );
// [ 0, 1, "FOO", 3 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment