简体   繁体   中英

How to push values to new array by comparing 2 arrays

I have 2 arrays.

selectedgroup = [ ID:1, selected:true,
                  ID:2, selected:false,
                  ..... ]

The 2nd array is,

 mainarr=[ ID:1, mainid:25, name:ser, pID:545,
            ID:2, mainid:84, name:ferdi, pID:678,
            ID:3, mainid:88, name:ferSER, pID:656,
              ....]

I want to check if mainarr contains an element of selectedgroup . The unique id in both arrays is ID . Then I want to push it on a new array. How can I do this?

Try this

var newarr =[];
for(i=0; i<mainarr.length; i++){
 for(j=0;j<selectedgroup.length; j++){
  if(mainarr[i].ID = selectedgroup[j].ID)
  {
  newarr.push(mainarr[i]);
  }
 }
}

Assuming your array is in following valid format. Iterate over your mainarr and check if ID of mainarr matches the ID of selectedgroup array then simply push the object into new array. Like following:

 var selectedgroup = [ {ID:1, selected:true},{ID:2, selected:false} ]; var mainarr = [ {ID:1, mainid:25, name:'ser', pID:545},{ID:2, mainid:84, name:'ferdi', pID:678},{ID:3, mainid:88, name:'ferSER', pID:656}]; var newArray = []; mainarr.forEach(function(mainObject) { for (let i=0; i<selectedgroup.length; i++){ if(selectedgroup [i].ID === mainObject.ID){ newArray.push(mainObject); } } }); console.log(newArray);

I think use of filter, is a better method than use of forEach. So I just include this solution for reference.

 var selectedgroup = [ {ID:1, selected:true},{ID:2, selected:false} ]; var mainarr = [ {ID:1, mainid:25, name:'ser', pID:545},{ID:2, mainid:84, name:'ferdi', pID:678},{ID:3, mainid:88, name:'ferSER', pID:656}]; const selectedgroupIds = selectedgroup.map(selected => selected.ID); const newarr = mainarr.filter(main => { return selectedgroupIds.indexOf(main.ID) !== -1; })

Array.prototype.filter() method creates a new array with all elements that pass the test implemented by the provided function. Like following:

var selectedgroup = [ {ID:1, selected:true},{ID:2, selected:false} ];
var mainarr = [ {ID:1, mainid:25, name:'ser', pID:545},{ID:2, mainid:84, name:'ferdi', pID:678},{ID:3, mainid:88, name:'ferSER', pID:656}];
var newarr = mainarr.filter(function(eachArr){
              for (var i = 0; i < selectedgroup.length; i++){
                 if(selectedgroup [i].ID === eachArr.ID){
                   return true;
                 } }})

If selectedgroup array can have many values, you can use map to get all ids first to avoid inner loop and then filter with them as @CHANist said.

For your reference: Array.prototype.map() . Thus, the code would become

var selectedgroup = [ {ID:1, selected:true},{ID:2, selected:false} ];
var mainarr = [ {ID:1, mainid:25, name:'ser', pID:545},{ID:2, mainid:84, name:'ferdi', pID:678},{ID:3, mainid:88, name:'ferSER', pID:656}];
var ids = selectedgroup.map(function(obj){ return obj.ID});
var newarr = mainarr.filter(function(eachArr){
                for (var i = 0; i < selectedgroup.length; i++){
                   if(ids.indexOf(eachArr.ID) !== -1){
                      return true;
                    } }})

Use below code,

var selectedgroup = [
                      {ID:1, selected:true},
                      {ID:2, selected:false}
                  ];
var mainarr = [
                  {ID:1, mainid:25, name:'ser', pID:545},
                  {ID:2, mainid:84, name:'ferdi', pID:678},
                  {ID:3, mainid:88, name:'ferSER', pID:656}
              ];
var newArray=[];
for (var i = 0, l2 = mainarr .length; i < l2; i++) {
    for (var j = 0, l1 = selectedgroup .length; j < l1; j++) {
        if(mainarr [i].ID===selectedgroup [j].ID){
            newArray.push(mainarr [i]);
        }
    }
 }
 console.log(newArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM