Q:

Write a JavaScript program to filter an array of objects based on a condition while also filtering out unspecified keys

0

Write a JavaScript program to filter an array of objects based on a condition while also filtering out unspecified keys.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const reducedFilter = (data, keys, fn) =>
  data.filter(fn).map(el =>
    keys.reduce((acc, key) => {
      acc[key] = el[key];
      return acc;
    }, {})
  );
const data = [
  {
    id: 1,
    name: 'john',
    age: 24
  },
  {
    id: 2,
    name: 'mike',
    age: 50
  }
];

console.log(reducedFilter(data, ['id', 'name'], item => item.age > 24));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now