美文网首页
类数组对象转换为数组的 5 种方法

类数组对象转换为数组的 5 种方法

作者: 弹指一挥间_e5a3 | 来源:发表于2020-04-26 10:51 被阅读0次
// 1. slice
Array.prototype.slice.call(arrayLike, 0); // ["name", "age", "sex"] 
Array.prototype.slice.call(arrayLike); // ["name", "age", "sex"] 
// 2. splice
Array.prototype.splice.call(arrayLike, 0); // ["name", "age", "sex"] 
// 3. ES6 Array.from
Array.from(arrayLike); // ["name", "age", "sex"] 
// 4. apply
Array.prototype.concat.apply([], arrayLike)
// 5. map
Array.prototype.map.call(arrayLike, function(item){
    return item.toUpperCase();
}); 

相关文章

网友评论

      本文标题:类数组对象转换为数组的 5 种方法

      本文链接:https://www.haomeiwen.com/subject/odxsyhtx.html