zoukankan      html  css  js  c++  java
  • 类数组转数组的一些方法

    数组:
    var array = ['name', 'age', 'sex']; 类数组:
    var arrayLike = { 0: 'name', 1: 'age', 2: 'sex', length: 3 }

    如果类数组就是任性的想用数组的方法怎么办呢?

    既然无法直接调用,我们可以用 Function.call 间接调用:

    var arrayLike = {0: 'name', 1: 'age', 2: 'sex', length: 3 }
    
    Array.prototype.join.call(arrayLike, '&'); // name&age&sex
    
    Array.prototype.map.call(arrayLike, function(item){
        return item.toUpperCase();
    }); 
    数组转类数组的四种方法:
    Array.prototype.slice.call(arrayLike, 0); // ["name", "age", "sex"] 
    // slice可以做到类数组转数组
    Array.prototype.slice.call(arrayLike); // ["name", "age", "sex"] 

    Array.prototype.splice.call(arrayLike,0)
    Array.from(arrayLike); 

    Array.prototype.concat.
    apply([], arrayLike)
    文章来源:https://github.com/mqyqingfeng/Blog/issues/14


     
  • 相关阅读:
    矩阵求导笔记
    Saliency map实现
    lime用法浅析
    LeetCode 989. 数组形式的整数加法
    题解:[P1009 阶乘之和]
    P1008三连击
    vim-2
    c#基础零碎记录
    asp.net core MySQL 數據遷移
    虚函数
  • 原文地址:https://www.cnblogs.com/xuniannian/p/9829019.html
Copyright © 2011-2022 走看看