Menu

gitpiper

frequencies javascript Code Snippet in 2023

arrayobjectintermediate

Last Updated: 3 February 2023

Creates an object with the unique values of an array as keys and their frequencies as the values.

  • Use Array.prototype.reduce() to map unique values to an object's keys, adding to existing keys every time the same value is encountered.
const frequencies = arr => arr.reduce((a, v) => { a[v] = a[v] ? a[v] + 1 : 1; return a; }, {});
frequencies(['a', 'b', 'a', 'c', 'a', 'a', 'b']); // { a: 4, b: 2, c: 1 } frequencies([...'ball']); // { b: 1, a: 1, l: 2 }

javascript snippet similar to frequencies For You in February 2023

Subscribe to our Newsletter

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

© 2023 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! ✌️