I am trying to break an larger array into smaller chunks.
Here is an example:
var data = [ { number: '184805871' },
{ number: '3418400730' },
{ number: '2047901973' },
{ number: '5038880529' },
{ number: '5040293507' },
{ number: '5044243187' },
{ number: '5078304520' },
{ number: '5085466763' },
{ number: '5145133307' },
{ number: '5197540781' },
{ number: '5199065039' },
{ number: '5208120208' },
{ number: '5212123861' },
{ number: '5212383329' },
{ number: '5214353364' },
{ number: '5229836037' }
];
My array is much longer than this, this is just an example. So I am familar with the array_chunk function in PHP and found it in php.js
function array_chunk (input, size, preserve_keys) {
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;
}
Is there an easier way to do this than that?
Here is my code:
var chunks = array_chunk(data, 3);
jQuery.each(chunks, function(index, value) {
console.log(index + ': ' + value);
});
How can I get the value from number to console.log instead of [Object]? I tried value[0] etc but it isn't working..
For each array that is split into 3 per array, I need to get the index and the value from the array.
How can I do this?