Instantly share code, notes, and snippets.

View groupBy.semantic.js
const group = (obj, field) => obj[field]
const addValue = (result, obj, field) =>
result[obj[field]] || []
const addToGroup = (result, obj, field) =>
[...(addValue(result, obj, field)), obj]
const toNewGroupUsing = (field) => (result, obj) => ({
...result,
View filter09.js
const toNewGroup = (field) => (result, obj) => ({
...result,
[obj[field]]: [...(result[obj[field]] || []), obj]
})
const group = (list) => (field) =>
list.reduce(toNewGroup(field), {})
const groupBy = group(list)
View filter08.js
const toNewGroup = (field) => (result, obj) => ({
...result,
[obj[field]]: [...(result[obj[field]] || []), obj]
})
const groupBy = (list, field) =>
list.reduce(toNewGroup(field), {})
console.log(groupBy(list, "level"))
View filter07.js
const toNewGroup = (field) => (result, obj) => ({
...result,
[obj[field]]: [...(result[obj[field]] || []), obj]
})
const groups = list.reduce(toNewGroup("level"), {})
View filter06.js
const toNewGroup = (field) => (result, obj) => {
result[obj[field]] = (!result[obj[field]])
? [obj]
: [...result[obj[field]], obj]
return result
}
const groups = list.reduce(toNewGroup("level"), {})
console.log(groups)
View filter05.js
const toNewGroup = (field) => (result, obj) => {
result[field] = (!result[field])
? [obj]
: [...result[field], obj]
return result
}
const groups = list.reduce(toNewGroup("level"), {})
console.log(groups)
View filter04.js
{ senior:
[ { id: 1,
name: 'Suissa',
active: true,
level: 'senior',
tags: [Array] },
{ id: 2,
name: 'Bella',
active: true,
level: 'senior',
View filter03.js
const plenos = list.filter(toGroup("level", "pleno"))
console.log({plenos})
/**
{ plenos:
[ { id: 4,
name: 'John',
level: 'pleno',
active: false,
tags: [Array] } ] }
View filter02.js
const toGroup = (field, value) => (obj) =>
obj[field] === value
const filtered = list.filter(toGroup("level", "senior"))
console.log({filtered})
/**
{ filtered:
[ { id: 1,
name: 'Suissa',
View filter01.js
const obj1 = {
id: 1,
name: "Suissa",
active: true,
level: "senior",
tags: [
"teacher",
"dev",
"js"
]