Array.from()
Array.from() 方法从一个类似数组或可迭代对象中创建一个新的数组实例。
此方法是ES6方法。
Array.from(arrayLike[, mapFn[, thisArg]])
arrayLike
想要转换成数组的伪数组对象或可迭代对象。
mapFn (可选参数)
如果指定了该参数,新数组中的每个元素会执行该回调函数。
thisArg (可选参数)
可选参数,执行回调函数 mapFn 时 this 对象。
返回一个新的数组实例
Array.from() 可以通过以下方式来创建数组对象:
-
伪数组对象(拥有一个 length 属性和若干索引属性的任意对象)
- 可迭代对象(可以获取对象中的元素,如 Map和 Set 等)
Array.from() 方法有一个可选参数 mapFn,让你可以在最后生成的数组上再执行一次 map 方法后再返回。也就是说 Array.from(obj, mapFn, thisArg) 就相当于 Array.from(obj).map(mapFn, thisArg), 除非创建的不是可用的中间数组。 这对一些数组的子类,如 typed arrays 来说很重要, 因为中间数组的值在调用 map() 时需要是适当的类型。
from() 的 length 属性为 1 ,即Array.from.length = 1。
在 ES2015 中, Class 语法允许我们为内置类型(比如 Array)和自定义类新建子类(比如叫 SubArray)。这些子类也会继承父类的静态方法,比如 SubArray.from(),调用该方法后会返回子类 SubArray 的一个实例,而不是 Array 的实例。
Array.from('foo'); // ["f", "o", "o"] let s = new Set(['foo', window]); Array.from(s); // ["foo", window] let m = new Map([[1, 2], [2, 4], [4, 8]]); Array.from(m); // [[1, 2], [2, 4], [4, 8]] function f() { return Array.from(arguments); } f(1, 2, 3); // [1, 2, 3]
数组去重合并
function combine(){ let arr = [].concat.apply([], arguments); //没有去重复的新数组 return Array.from(new Set(arr)); } var m = [1, 2, 2], n = [2,3,3]; console.log(combine(m,n));// [1, 2, 3]
Polyfill
// Production steps of ECMA-262, Edition 6, 22.1.2.1 // Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from if (!Array.from) { Array.from = (function () { var toStr = Object.prototype.toString; var isCallable = function (fn) {//isCallable判断一个值是否是一个函数 return typeof fn === 'function' || toStr.call(fn) === '[object Function]'; }; var toInteger = function (value) {//toInteger将一个值转换成整数 var number = Number(value);//number强制转换 if (isNaN(number)) { return 0; }//如果是NaN,返回0 if (number === 0 || !isFinite(number)) { return number; }//如果等于0或者是无限,返回 return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));//保留正负号,然后用Math.floor去掉小数部分,返回 }; var maxSafeInteger = Math.pow(2, 53) - 1;//最大安全数字 var toLength = function (value) {//将一个值转换成合法的数组length var len = toInteger(value); return Math.min(Math.max(len, 0), maxSafeInteger); }; // The length property of the from method is 1. return function from(arrayLike/*, mapFn, thisArg */) { // 1. Let C be the this value. var C = this;//存下this // 2. Let items be ToObject(arrayLike). var items = Object(arrayLike);//将arrayLike转换成对象 // 3. ReturnIfAbrupt(items). if (arrayLike == null) {//如果arrayLike是空,抛出错误 throw new TypeError("Array.from requires an array-like object - not null or undefined"); } // 4. If mapfn is undefined, then let mapping be false. //如果mapFn没有传递,那就赋值为undefined var mapFn = arguments.length > 1 ? arguments[1] : void undefined; var T; if (typeof mapFn !== 'undefined') {//如果有mapFn // 5. else // 5. a If IsCallable(mapfn) is false, throw a TypeError exception. if (!isCallable(mapFn)) {//判断mapFn是否是合法函数,不是的话抛出错误 throw new TypeError('Array.from: when provided, the second argument must be a function'); } // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined. if (arguments.length > 2) {//T是自定义this值 T = arguments[2]; } } // 10. Let lenValue be Get(items, "length"). // 11. Let len be ToLength(lenValue). var len = toLength(items.length);//结果数组的长度 // 13. If IsConstructor(C) is true, then // 13. a. Let A be the result of calling the [[Construct]] internal method // of C with an argument list containing the single item len. // 14. a. Else, Let A be ArrayCreate(len). //如果this对象是一个构造函数,那么A就是新的C实例化对象,否则是新的数组实例 var A = isCallable(C) ? Object(new C(len)) : new Array(len); // 16. Let k be 0. var k = 0;//循环索引 // 17. Repeat, while k < len… (also steps a - h) var kValue; while (k < len) {//遍历arrayLike kValue = items[k];//当前值 if (mapFn) {//如果有mapFn,结果数组的当前值就是调用mapFn的返回值 A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k); } else {//如果没有mapFn,结果数组的当前值就是arrayLike当前循环值 A[k] = kValue; } k += 1; } // 18. Let putStatus be Put(A, "length", len, true). A.length = len; // 20. Return A. return A; }; }()); }