Useful functions for working with arrays:
- Range
- Flatten (recursively)
- Chunk
- Remove duplicates
- Remove duplicates by object property
- Group by object property
function chunk (arr, size) { | |
return arr | |
.reduce((acc, _, i) => | |
(i % size) | |
? acc | |
: [...acc, arr.slice(i, i + size)] | |
, []) | |
} | |
// USAGE | |
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
const result = chunk(numbers, 3) | |
console.log(result) | |
// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] |
function flatten (arr) { | |
return arr | |
.reduce((acc, cur) => | |
Array.isArray(cur) | |
? acc.concat(flatten(cur)) | |
: acc.concat(cur) | |
, []) | |
} | |
// USAGE | |
const fruits = ['apple', ['pear', ['kiwi', 'orange']], ['melon']] | |
const result = flatten(fruits) | |
console.log(result) | |
// ['apple', 'pear', 'kiwi', 'orange', 'melon'] |
function groupByObjectProp (arr, prop) { | |
return arr | |
.reduce((acc, cur) => { | |
const key = cur[prop] | |
acc[key] = acc[key] || [] | |
acc[key].push(cur) | |
return acc | |
}, {}) | |
} | |
// USAGE | |
const foods = [ | |
{ name: 'apple', category: 'fruits' }, | |
{ name: 'pear', category: 'fruits' }, | |
{ name: 'broccoli', category: 'vegetables' }, | |
{ name: 'potato', category: 'vegetables' }, | |
{ name: 'almond', category: 'nuts' }, | |
{ name: 'kiwi', category: 'fruits' }, | |
{ name: 'orange', category: 'fruits' }, | |
{ name: 'cabbage', category: 'vegetables' }, | |
{ name: 'melon', category: 'fruits' }, | |
{ name: 'peacan', category: 'nuts' }, | |
{ name: 'walnut', category: 'nuts' } | |
] | |
const result = groupByObjectProp(foods, 'category') | |
console.log(result) | |
/* | |
{ | |
fruit: [ | |
{ name: 'apple', category: 'fruits' }, | |
{ name: 'pear', category: 'fruits' }, | |
{ name: 'kiwi', category: 'fruits' }, | |
{ name: 'orange', category: 'fruits' }, | |
{ name: 'melon', category: 'fruits' } | |
], | |
vegetable: [ | |
{ name: 'broccoli', category: 'vegetables' }, | |
{ name: 'potato', category: 'vegetables' }, | |
{ name: 'cabbage', category: 'vegetables' } | |
], | |
nut: [ | |
{ name: 'almond', category: 'nuts' }, | |
{ name: 'peacan', category: 'nuts' }, | |
{ name: 'walnut', category: 'nuts' } | |
] | |
} | |
*/ |
function range (start, end) { | |
return Array(end - start + 1) | |
.fill() | |
.map((_, i) => i + start) | |
} | |
function range2 (start, end) { | |
return Array.from( | |
{ length: end - start + 1 }, | |
(_, i) => i + start | |
) | |
} | |
function range3 (start, end) { | |
return [...Array(end - start + 1).keys()] | |
.map((_, i) => i + start) | |
} | |
// USAGE | |
const result = range(5, 10) | |
console.log(result) | |
// [5, 6, 7, 8, 9, 10] |
function removeDupsByObjectProp (arr, prop) { | |
return arr | |
.filter((item, i, self) => | |
i === self.findIndex(found => found[prop] === item[prop]) | |
) | |
} | |
// USAGE | |
const foods = [ | |
{ name: 'apple', category: 'fruits' }, | |
{ name: 'apple', category: 'fruits' }, | |
{ name: 'apple', category: 'fruits' }, | |
{ name: 'pear', category: 'fruits' }, | |
{ name: 'kiwi', category: 'fruits' }, | |
{ name: 'kiwi', category: 'fruits' }, | |
{ name: 'broccoli', category: 'vegetables' }, | |
{ name: 'broccoli', category: 'vegetables' }, | |
{ name: 'potato', category: 'vegetables' } | |
] | |
const result = removeDupsByObjectProp(foods, 'name') | |
console.log(result) | |
/* | |
[ | |
{ name: 'apple', category: 'fruits' }, | |
{ name: 'pear', category: 'fruits' }, | |
{ name: 'kiwi', category: 'fruits' }, | |
{ name: 'broccoli', category: 'vegetables' }, | |
{ name: 'potato', category: 'vegetables' } | |
] | |
*/ |
function removeDups (arr) { | |
return arr | |
.filter((item, i, self) => self.indexOf(item) === i) | |
} | |
// USAGE | |
const fruits = ['apple', 'apple', 'apple', 'pear', 'kiwi', 'kiwi'] | |
const result = removeDups(fruits) | |
console.log(result) | |
// ['apple', pear', 'kiwi'] |