Last active
June 1, 2017 15:56
-
-
Save vino24/72dbe64697005aac87325df9c988daeb to your computer and use it in GitHub Desktop.
类数组转数组
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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