how to filter array of object from array

questionshow to filter array of object from array
Jaya asked 2 years ago

hi
i have an array of object i want to find all data having in other array.

2 Answers
admin Staff answered 2 years ago

Hi pravesh,
if you share me dummy data i can help you more. might be below example helpful for you.

const cities = ['mumbai', 'chennai','pune','delhi']
const source = Rx.Observable.from([{_id: '123', "patient": "ajay","clinic":{
"city":"mumbai"}},
                                   {_id: '1234', "patient": "ajay2","clinic":{
"city":"chennai"}},
                                   {_id: '12345', "patient": "ajay3","clinic":{
"city":"delhi"}}
                                  ]);



source.filter(app => cities.find(dd=> dd== app.clinic.city)) 
.subscribe(data=>{
  console.log('Result',data)
})
Output will be as
"Result"
[objectObject]{
  _id: "123",
  clinic: [object Object] {
    city: "mumbai"
  },
  patient: "ajay"
}
"Result"
[objectObject]{
  _id: "1234",
  clinic: [object Object] {
    city: "chennai"
  },
  patient: "ajay2"
}
"Result"
Preeti Vedarthi answered 6 months ago

let masterData =[{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data1″},
{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data2″},{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data3″},{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data4″},{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data5″}];
let selectedData =[{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data1″},
{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data4″}];
Expected Result:
After Filter Master Data from Selected Data 
[{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data2″},{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data3″},{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data5″}];
 
Code:

let result =masterData.filter(m => {
            if (!selectedData.find(b => b._id == m._id)) {
                return m;
            }
        });

Output:
[{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data2″},{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data3″},{“_id”:”5c7dff521495151c1941fba3″,”name”:”Data5″}];