1

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 ?

3 Answers3

2

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.

mekwall
  • 28,614
  • 6
  • 75
  • 77
  • 1
    Please note that this solution is destructive of `data` so only OK if `data` is not needed later in the code. – Beetroot-Beetroot Jun 23 '13 at 16:07
  • if output going be [[1,2,3,4],[5,6,7,8],[9,10]] then getting warning as "DataTables warning (table id = 'products'): Requested unknown parameter '2' from the data source for row 1". Is there any way to handle ? –  Jun 23 '13 at 16:42
  • 1
    I expect you just need ensure that the last group contains 4 strings, so if it's short, pad it with blanks. `if(output.length) { var lastGroup = output.slice(-1); for (var i=0; i<4; i++) { lastGroup[i] = lastGroup[i] || ''; } }`. – Beetroot-Beetroot Jun 23 '13 at 17:52
  • @HelloWorld I've updated the answer to ensure every group contains 4 items each. – mekwall Jun 23 '13 at 21:20
0

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));
Community
  • 1
  • 1
krishwader
  • 11,341
  • 1
  • 34
  • 51
0

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' ] 
//   ]
Christian Landgren
  • 13,127
  • 6
  • 35
  • 31