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
算,取的数组结束的所有元素
网友评论