firasKoubaa
firasKoubaa

Reputation: 6867

Javascript : How to filter an array's contents into two resulting arrays

i have an array which contains simple numbers (ids) :

idsGroup : [0,1,5,66,88,10,12.....]

i have also another "each" loop which is fetchi for every id in that table its value , so i'm injecting with every id a string : (ids array and the rendered list from the each loop , have the same length)

to obtain finally an array like that :

idsGroup : [0-state1,1-state2,5-state1,66-state1,88-state2.....] 

her is to complete code :

$('#select option:selected').each(function () {
                var typeGroup = $j(this).data('type');
                console.log(typeGroup);
                idsGroups[i] = idsGroups[i]+"-"+typeGroup;
                i=i+1;
});

As there is only two possible states : state1 and state2 ;

i wanna obtain two usable table where i'm hathering for each state , all its ids :

Like That:-

state1=[0,5,66...]
state2=[1,88.....]

AND OF COURSE i should be able to use them outside the "each" loop

Suggestions ??

Upvotes: 0

Views: 47

Answers (1)

Brent Boden
Brent Boden

Reputation: 579

something like this ?

var idsGroup = ["0-state1","1-state2","5-state1","66-state1","88-state2"];

var state1 = [];
var state2 = [];

for(var i = 0; i < idsGroup.length; i++){
    if(idsGroup[i].indexOf("state1") > 0){
        state1.push(idsGroup[i].replace('-state1',''));
    }else{
        state2.push(idsGroup[i].replace('-state2',''));
    }
}

Upvotes: 1

Related Questions