Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
partial.lenses: query array by sub array object property
// REPL: https://calmm-js.github.io/partial.lenses/playground.html
// discussion: https://gitter.im/calmm-js/chat?at=58ecf9c6bdf4acc1124c504f
const list =
{
tasks: [ { id: 2
, state: 'todo'
, users: [ { id: 1 } ]
}
, { id: 2
, state: 'todo'
, users: [ { id: 1 }
, { id: 5 }
]
}
, { id: 4
, state: 'todo'
, users: [ { id: 2 } ]
}
]
}
const hasUserWithID = id =>
L.when( L.any( R.equals( id )
, [ 'users'
, L.elems
, 'id'
]
)
)
const tasksForUser = id =>
[ 'tasks'
, L.elems
, hasUserWithID( id )
]
// return an array of tasks having user.id === 1
L.collect( tasksForUser( 1 ), list )
// set task.state to 'audit' on all tasks having user.id === 1
L.set( [ tasksForUser( 1 ), 'state' ], 'audit', list )
// partial.lenses: // REPL: https://calmm-js.github.io/partial.lenses
// calmm-js: https://github.com/calmm-js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.