0

I couldn't find any solution that would suit for me regarding this question, so excuse me if duplicating.

I want to go through all .children() items and if some of those items meet condition I want to save those items in one array. Here's the code to show exactly what I mean:

    var items = [],
        allitems = $('.iconImg').children(),
        obj;


    // console.log(allitems);

    $('#next4').on('click', function(e){

        e.preventDefault();

        for(var i=0; i<allitems.length; i++){

            var currItem = $(allitems[i]).attr('id'),
                currItemPosLeft = parseInt($('#'+currItem).css('left')),
                itemOffset = getOffset('#'+currItem),
                items = [],
                arrToSend = [];

                // console.log(currItem, '=>', currItemPosLeft);    

            if(currItemPosLeft >= 405){

                obj = {

                    obId: currItem,
                    obPos: itemOffset

                }


                items.push(obj);
                console.log(items);

            }                   


        }

This code gives me separately number of arrays of objects that met condition, but what I would like is one array to store these objects.

How can I do this?

Thanks

James W.
  • 19
  • 1
  • 4
  • Possible duplicate: http://stackoverflow.com/questions/9151729/how-to-get-children-array-of-an-element-in-jquery – tomloprod Dec 25 '14 at 10:39

2 Answers2

0

Ok, I solved it. I'm not sure why I complicated this much, it's super easy.
So, the code would be:

    var items = [],
    allitems = $('.iconImg').children().toArray(),
    obj;


    // console.log(allitems);

    $('#next4').on('click', function(e){

        e.preventDefault();

        $(allitems).each(function(i){

            var currItem = $(allitems[i]).attr('id'),
                currItemPosLeft = parseInt($('#'+currItem).css('left')),
                itemOffset = getOffset('#'+currItem);


            // console.log(currItem, '=>', currItemPosLeft);

            obj = {

                odId: currItem,
                obPos: itemOffset

            };

            if(currItemPosLeft >= 405){

                items.push(obj);

            }




        })

        console.log(items);


    })
    //end of #next4 click
James W.
  • 19
  • 1
  • 4
0

just a demo to do things;

var testData = [{id:1,data:'abc'},{id:2,data:'efg'},{id:3,data:'pqr'},{id:4,data:'xyz'}]

var res = []

testData.forEach(function(o){ 
       if(o.id % 2 == 0) { //your condition..
          res.push({nid:o.id, ndata: o.data}) 
       } 
});

DEMO: http://jsfiddle.net/dpst36xe/

A.T.
  • 24,694
  • 8
  • 47
  • 65