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

remove the object if its field does not match with the other aray

let array = [
            {name: "60", origin: "tcs"},
            {name: "70", origin: "cfs"},
            {name: "80", origin: "ehg"},
    ]
undefined
let def = [
            {id: "60", testorigin: "tcs"},
            {id: null, testorigin: "cfs"},   // this line should be removed
            {id: "80", testorigin: "ehg"},
]

output

[
    {name: "60", origin: "tcs"},
    {name: "80", origin: "ehg"},

]

I just want to remove the second element of def because its id is null and its not matching with the name of array.

tried many way, but I failed, Thanks in advance.

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

>Solution :

You can do it like this:

let array = [
  {name: "60", origin: "tcs"},
  {name: "70", origin: "cfs"},
  {name: "80", origin: "ehg"},
 ];
 
 let def = [
  {id: "60", testorigin: "tcs"},
  {id: null, testorigin: "cfs"},
  {id: "80", testorigin: "ehg"},
];

const result = array.filter(item => def.map(d => d.id).includes(item.name));
console.log('Result: ', result);
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