Menu

gitpiper

partition javascript Code Snippet in 2022

arrayobjectintermediate

Last Updated: 25 September 2022

Groups the elements into two arrays, depending on the provided function's truthiness for each element.

  • Use Array.prototype.reduce() to create an array of two arrays.
  • Use Array.prototype.push() to add elements for which fn returns true to the first array and elements for which fn returns false to the second one.
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); // [ // [{ user: 'fred', age: 40, active: true }], // [{ user: 'barney', age: 36, active: false }] // ]

javascript snippet similar to partition For You in September 2022

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2022 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️