Q:

Write a JavaScript program to group the elements into two arrays, depending on the provided function's truthiness for each element

0

Write a JavaScript program to group the elements into two arrays, depending on the provided function's truthiness for each element.

All Answers

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

const partition = (arr, fn) =>
  arr.reduce(
    (acc, val, i, arr) => {
      acc[fn(val, i, arr) ? 0 : 1].push(val);
      return acc;
    },
    [[], []]
  );
const users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }];
partition(users, o => o.active);
console.log(users);

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