I'm getting array of data from server, but after come to jquery datatable i needs multidimention array .Is there any way to make it in jquery itself beflore pass it to datatables ?
My input format is :
["computer","program","laptop","monitor","mouse","keybord","cpu","harddrive"......]
Expected format :
[["computer","program","laptop","monitor"],["mouse","keybord","cpu","harddrive"],[....],[....]........]
Is there any method to parse data format ?
-
3is it always arrays of 2?Hogan– Hogan2013年06月23日 15:37:08 +00:00Commented Jun 23, 2013 at 15:37
-
No , always going to be 4.user2505078– user25050782013年06月23日 15:41:38 +00:00Commented Jun 23, 2013 at 15:41
-
2So always 4 transforming to 2 + 2?Beetroot-Beetroot– Beetroot-Beetroot2013年06月23日 15:43:12 +00:00Commented Jun 23, 2013 at 15:43
-
2And why jQuery? This a pure javascript problem. Question should be retagged.Beetroot-Beetroot– Beetroot-Beetroot2013年06月23日 15:44:44 +00:00Commented Jun 23, 2013 at 15:44
-
2Err what? You are saying the input is an array of elements with 4 elements and you want to spit each array element? Please explain in the original question.Hogan– Hogan2013年06月23日 15:46:46 +00:00Commented Jun 23, 2013 at 15:46
3 Answers 3
It doesn't take much more than a simple while loop to transform the array.
// This is the original data we get from the server
var input = ["computer","program","laptop","monitor","mouse","keybord","cpu","harddrive"];
// Make a copy of the input, so we don't destroy it
var data = input.slice(0);
// This is our output array
var output = [], group;
// A while loop will transform the plain array into a multidimensional array
while (data.length > 0) {
// Take the first four items
group = data.splice(0, 4);
// Make sure the group contains 4 items, otherwise pad with empty string
while (group.length < 4) {
group.push("");
}
// Push group into the output array
output.push(group);
}
// output = [["computer","program","laptop","monitor"],["mouse","keybord","cpu","harddrive"]]
Update: Beetroot-Beetroot's comment is no longer valid since we create a copy of the input.
4 Comments
data so only OK if data is not needed later in the code.if(output.length) { var lastGroup = output.slice(-1); for (var i=0; i<4; i++) { lastGroup[i] = lastGroup[i] || ''; } }.I found this beautiful question sometime ago when i had a similar problem. This is a solution based (erm.. ripped out from there) on that :
var a = ["computer", "program", "laptop", "monitor", "mouse", "keybord", "cpu", "harddrive", "tablet"],
n = a.length / 4,
len = a.length,
out = [],
i = 0;
while (i < len) {
var size = Math.ceil((len - i) / n--);
out.push(a.slice(i, i + size));
i += size;
}
alert(JSON.stringify(out));
Comments
Message from the future ;) - now we have reduce:
function groupArray(array, groupSize) {
return array.reduce((a, b, i) => {
if (!i || !(i % groupSize)) a.push([])
a.slice(-1).pop().push(b)
return a
}, [])
}
console.log(groupArray(input, 4))
// [
// [ 'computer', 'program', 'laptop', 'monitor' ],
// [ 'mouse', 'keybord', 'cpu', 'harddrive' ]
// ]