0

a collection is returning 11 items as follows;

   ( 1, "Ball", "Result1") 
   ( 2, "Ball", " Result2") 
   ( 3, "Ball", " Result3") 
   ( 4, "Ball", " Result4") 
   ( 5, "Ball", " Result5") 
   ( 6, "Ball", " Result6") 
   ( 7, "Ball", " Result7") 
   ( 8, "Ball", " Result8") 
   ( 9, "Pool", " Pool 1") 
   ( 10, "Pool", " Pool 2") 
   ( 11, "Pool", " Pool 3") 

I want to store them, group them as four items.. so that my array looks like this

var data = [];
                    data.push({
                    myclass: "First4",
                    schedule: [ {
                        id : '1',
                        gameType: 'Ball',

                        result: 'Result11'
                    }, {
                        id: '2',
                        gameType: 'Ball',

                        result: 'Result2'

                    },...........  ]



                });
                                    //second group
                data.push({
                    divClass : "second4",
                    items : [ {
                        id : '5'
                        gameType: 'Ball',
                        result: 'Result5'
                    }, {
                        id : ''
                        gameType: 'Ball',
                        result: 'Result6

                    } ]
                });

how can i write a for loop so that i can achieve the same result dynamically instead of writing the push manually

for(var i = 0; i < collLength; i++){
// do push 1 with first four  //data.push(class, sheculde first 4 items, result)
// do second push with second four
// do third push with the remaining

  }
6
  • Do you honestly want to name your classes "first4", "second4", etc? It would seem simpler to class them by their array index dynamically "group0", "group1", etc where the number comes from the array index. Commented Oct 26, 2012 at 3:02
  • ...but for your loop, just change i++ to i+=4, and then access the items at i, i+1, i+2, i+3 in the loop. Commented Oct 26, 2012 at 3:04
  • i was just trying to make my question clear... What i need to do is, when i receive x number of items, in this case 11, i want to have x number of groups (in this case 3) where each group holds maximum of 4 of the items. I just couldn't write the right for loop to do it dynamically... Commented Oct 26, 2012 at 3:06
  • Yeah, just make the loop increment by 4, and access the items at the current index and the next three indexes. Just adjust the .push() however you want it, but it looks like you've got a handle on that part. Commented Oct 26, 2012 at 3:08
  • Thanks for the responses. They all are working, but is there a away to handle this without using "slice"? Commented Oct 26, 2012 at 5:22

4 Answers 4

1
var data = [];

for(var i = 0; i < collLength; i+=4) {
    data.push({
        divClass: "group" + (i / 4),
        items: collection.slice(i, i + 4).map(function(item) {
            return {id:item[0], gameType:item[1], result:item[2]};
        })
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0
var indata = [...]; // this is filled with your list of objects.
var outdata = [ ];

var n = indata.length;

for (var i = 0; i < n; i += 4)
{
    outdata.push({
        class: 'group' + outdata.length, 
        items: indata.slice(i, i + 4)
    });
}

Comments

0
var indata = [...]; // this is filled with your list of objects.
var outdata = [ ];

function mapper(element, i)
{
     var j = Math.floor(i / 4);

     if (outdata[j] == undefined)
         outdata[j] = { class: 'group' + j, items: [ ] };

     outdata[j].items.push(element);
}

indata.map(mapper);

Comments

0

Here's a way using .reduce().

var data = collection.reduce(function(arr, obj, i) {
    return i % 4 ? arr : 
                   arr.concat({
                      divClass: "group" + (i / 4),
                      items: collection.slice(i, i + 4).map(function(item) {
                          return {id:item[0], gameType:item[1], result:item[2]};
                      })
                   });
}, []);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.