1

I have 2 arrays of objects. They are

array1 = [{
  id:2,
  name:"person2"
},{
  id:3,
  name:"person3"
},
{
  id:4,
  name:"person4"
},
{
  id:5,
  name:"person5"
},
];
array2 = [
{
    empId:2,
    isdeleted:false
},
{
    empId:4,
    isdeleted:false
}, 
{   
    empId:3,
    isdeleted:true
}];

I need the object from array1 whose id matches with empId of array2 and having isdeleted false. Thanks in advance.

2
  • I have attempted it but I didn't get what i expected.Below is the query i have tried. Can u help me with this Roope. this.array1.filter(o => this.array2.find(o2 => o.id === o2.empId)) Commented Dec 4, 2018 at 10:39
  • Okay, you should include your efforts in the question. The first thing said behind the link is "It appears no attempt was made". Emphasis on "appears". If you do not give any indication of an attempt in your question, others can only assume that you have not attempted anything yourself. Commented Dec 4, 2018 at 10:43

4 Answers 4

1

You can use filter and some like this:

let array1 = [{
    id: 2,
    name: "person2"
  }, {
    id: 3,
    name: "person3"
  },
  {
    id: 4,
    name: "person4"
  },
  {
    id: 5,
    name: "person5"
  },
];

let array2 = [{
    empId: 2,
    isdeleted: false
  },
  {
    empId: 4,
    isdeleted: false
  },
  {
    empId: 3,
    isdeleted: true
  }
];

let filteredArray = array1.filter(a => array2.some(b => b.empId === a.id && !b.isdeleted));
console.log(filteredArray);

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

3 Comments

One more question is that if in array2 I have age as well. then if i want the resultant object as {id:some_id,name:some_name,age:some_age}. Is that possible?
You want to filter based on age as well?
I have edited the comment. Would you please look into it.
0

Try

let result = [];
array1.forEach(function(element1){
    array2.forEach(function(element2){
        if (element1.id === element2.empId && !element2.isdeleted){
            result.push(element);
        }
    });
});
console.log(result);

Comments

0

You can use Array.filter() together with find() to find the object with that condition in array2:

var array1 = [{
    id: 2,
    name: "person2"
  }, {
    id: 3,
    name: "person3"
  },
  {
    id: 4,
    name: "person4"
  },
  {
    id: 5,
    name: "person5"
  },
];
var array2 = [{
    empId: 2,
    isdeleted: false
  },
  {
    empId: 4,
    isdeleted: false
  },
  {
    empId: 3,
    isdeleted: true
  }
];

var res = array1.filter((obj1)=>{
  var exist = array2.find((obj2)=> (obj1.id == obj2.empId && !obj2.isdeleted));
  return exist;
});
console.log(res);

Comments

0

You could try something like this:

let array1 = [
    { id:2, name:"person2"},
    { id:3, name:"person3"},
    { id:4, name:"person4"},
    { id:5, name:"person5"}
];
  
let array2 = [
    { empId:2, isdeleted:false},
    { empId:4, isdeleted:false}, 
    { empId:3, isdeleted:true}
];

let result = array1.reduce((output, item) => {
    if (array2.find((item2) => !item2.isdeleted && item.id === item2.empId)) output.push(item);
    return output;
}, []);

console.log(result);

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.