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 can i check if an array of objects contains a key which is in array of strings js?

We have an array of objects like

const arr = [{id: "someId", name: {...}}, ...];
const skippedKeys = ["id"...]

How can i filtered the array of object based on skipped keys?
The result should be:

const result = [{name: {...}}, ...];

Also i don’t want to make a cycle inside the cycle.
the result also could be implemented using lodash library.
we should remove key with value as well.

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 :

const result = arr.map(obj =>
  Object.keys(obj).reduce(
    (res, key) => (
      skippedKeys.includes(key) ? res : {...res, [key]: obj[key]}
    ),
    {},
));
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