7
回答
怎么把js数组分割成多个数组?
【腾讯云】学生服务器套餐10元/月 >>>   

把JS数组分成多个数组!比如:每2个为一组,data里面有5个数组

var data = [{"user_id":"4""},{"user_id":"4""},{"user_id":"4""},{"user_id":"4""},{"user_id":"4""},{"user_id":"4""},]

                      分割后:  

                      0=> [{"user_id":"4"},{"user_id":"4""},]

                        1=>[{"user_id":"4""},{"user_id":"4""},]

                         2=>[{"user_id":"4""},]

php有

array_chunk

<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
?>

////

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
js数组怎么分割呢


<无标签>
举报
保密保密
发帖于5年前 7回/17K+阅
共有7个答案 最后回答: 2年前
var data = [{"user_id":"4"},{"user_id":"4"},{"user_id":"4"},{"user_id":"4"},{"user_id":"4"}];
var result = [];
for(var i=0,len=data.length;i<len;i+=2){
   result.push(data.slice(i,i+2));
}
/*
     chunk([1,2,3], 2)
     [ [1,2], [3] ]
     */
    var chunk = function (array, size) {
        var result = [];
        for (var x = 0; x < Math.ceil(array.length / size); x++) {
            var start = x * size;
            var end = start + size;
            result.push(array.slice(start, end));
        }
        return result;
    }



- -.这是json阿 你直接 data[0],data[1]这样就能取到每个数组阿..然后再push呗,把2个push到一个新的数组里..你alert(data[0][user_id]);就懂了..
function array_chunk (input, size, preserve_keys) {
  // *     example 1: array_chunk(['Kevin', 'van', 'Zonneveld'], 2);
  // *     returns 1: [['Kevin', 'van'], ['Zonneveld']]
  // *     example 2: array_chunk(['Kevin', 'van', 'Zonneveld'], 2, true);
  // *     returns 2: [{0:'Kevin', 1:'van'}, {2: 'Zonneveld'}]
  // *     example 3: array_chunk({1:'Kevin', 2:'van', 3:'Zonneveld'}, 2);
  // *     returns 3: [['Kevin', 'van'], ['Zonneveld']]
  // *     example 4: array_chunk({1:'Kevin', 2:'van', 3:'Zonneveld'}, 2, true);
  // *     returns 4: [{1: 'Kevin', 2: 'van'}, {3: 'Zonneveld'}]

  var x, p = '', i = 0, c = -1, l = input.length || 0, n = [];

  if (size < 1) {
    return null;
  }

  if (Object.prototype.toString.call(input) === '[object Array]') {
    if (preserve_keys) {
      while (i < l) {
        (x = i % size) ? n[c][i] = input[i] : n[++c] = {}, n[c][i] = input[i];
        i++;
      }
    }
    else {
      while (i < l) {
        (x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
        i++;
      }
    }
  }
  else {
    if (preserve_keys) {
      for (p in input) {
        if (input.hasOwnProperty(p)) {
          (x = i % size) ? n[c][p] = input[p] : n[++c] = {}, n[c][p] = input[p];
          i++;
        }
      }
    }
    else {
      for (p in input) {
        if (input.hasOwnProperty(p)) {
          (x = i % size) ? n[c][x] = input[p] : n[++c] = [input[p]];
          i++;
        }
      }
    }
  }
  return n;
}
--- 共有 1 条评论 ---
qilin892Thanks 3年前 回复

引用来自“arrowing”的答案

var data = [{"user_id":"4"},{"user_id":"4"},{"user_id":"4"},{"user_id":"4"},{"user_id":"4"}];
var result = [];
for(var i=0,len=data.length;i<len;i+=2){
   result.push(data.slice(i,i+2));
}

我在php端已经处理了,不在客户端处理了.主要是对JS不熟悉,obj.slice(strart,end)这个函数就是把一个数组分割多个数组的,条件是i+=n,

还有json和数组用法一样,某些人直接说这是json,不是数组..一票否决.唉

--- 共有 1 条评论 ---
Arrowing不是很明白你的意思 5年前 回复
顶部