Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter array of objects with array of objects in javascript

I want to remove an object in an array if the object is not included from another array

i have an array of objects

let programs = [
            {"id":1,"name":"BSIT","description":"Bachelor of Science in Information Technology","institute":"IC"},
            {"id":2,"name":"BSIS","description":"Bachelor of Science in Information System","institute":"IC"},
            {"id":3,"name":"BSED","description":"Bachelor of Secondary Education","institute":"ITED"},
            {"id":4,"name":"BSAF","description":"BACHELOR OF SCIENCE IN AGRO-FORESTRY","institute":"IAAS"}
        ]

and i want to remove the objects that exist in this array of objects

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let programs2 = [
            {"id":1,"name":"BSIT","description":"Bachelor of Science in Information Technology","institute":"IC"},
            {"id":2,"name":"BSIS","description":"Bachelor of Science in Information System","institute":"IC"},
            {"id":3,"name":"BSED","description":"Bachelor of Secondary Education","institute":"ITED"}
        ]

both are dynamic and they are fetched from database

i’ve tried filter but doesn’t work.

here is my approach

const ress =ref(programs.filter(element => {
            if(programs2.includes(element.id))
            {
                return element
            }
        }))

i used axios to fetch data

>Solution :

you can do something like this

const data1 = [
            {"id":1,"name":"BSIT","description":"Bachelor of Science in Information Technology","institute":"IC"},
            {"id":2,"name":"BSIS","description":"Bachelor of Science in Information System","institute":"IC"},
            {"id":3,"name":"BSED","description":"Bachelor of Secondary Education","institute":"ITED"},
            {"id":4,"name":"BSAF","description":"BACHELOR OF SCIENCE IN AGRO-FORESTRY","institute":"IAAS"}
        ]
        
const data2 =  [
            {"id":1,"name":"BSIT","description":"Bachelor of Science in Information Technology","institute":"IC"},
            {"id":2,"name":"BSIS","description":"Bachelor of Science in Information System","institute":"IC"},
            {"id":3,"name":"BSED","description":"Bachelor of Secondary Education","institute":"ITED"}
        ]
        
const filtered = data1.filter(({id}) => !data2.some(d => d.id === id))

console.log(filtered)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading