0

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?

1
  • 2
    The syntax here doesn't look valid. If these are arrays of objects, I recommend formatting them as such to eliminate any confusion about what we're working with. Beyond that, I recommend sharing your attempt as a minimal reproducible example and showing your exact expected output given these two structures you've shown. Thanks! Commented Jan 10, 2020 at 5:38

5 Answers 5

1

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);

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

Comments

1

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]);
  }
 }
}

Comments

0

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;
})

Comments

0

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;
                    } }})

Comments

0

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);

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.