美文网首页
js将一位数组分割成每三个一组

js将一位数组分割成每三个一组

作者: keknei | 来源:发表于2021-04-27 16:42 被阅读0次
    var data = [
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
      {name:'Liming',age:'25'},
    ]
    var result = [];
    for(var i=0;i<data.length;i+=3){
        result.push(data.slice(i,i+3));
    }
    

    巧妙的运用了slice方法不会改变原数组,并且返回一个新的数组,而且当slice(start,end)第二个end参数值大于数组length的时候,会按照数组length算,取的数组结束的所有元素

    相关文章

      网友评论

          本文标题:js将一位数组分割成每三个一组

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