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 ?

10
  • 3
    is it always arrays of 2? Commented Jun 23, 2013 at 15:37
  • No , always going to be 4. Commented Jun 23, 2013 at 15:41
  • 2
    So always 4 transforming to 2 + 2? Commented Jun 23, 2013 at 15:43
  • 2
    And why jQuery? This a pure javascript problem. Question should be retagged. Commented Jun 23, 2013 at 15:44
  • 2
    Err 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. Commented Jun 23, 2013 at 15:46

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Please note that this solution is destructive of data so only OK if data is not needed later in the code.
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 ?
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] || ''; } }.
@HelloWorld I've updated the answer to ensure every group contains 4 items each.
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));

Comments

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' ] 
//   ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.