I want to delete object from array by id using Ramda. For example:
const arr = [
{id: '1', name: 'Armin'},
{id: '2', name: 'Eren'}, <- delete this object
{id: '3', name: 'Mikasa'}
];
I want to delete object from array by id using Ramda. For example:
const arr = [
{id: '1', name: 'Armin'},
{id: '2', name: 'Eren'}, <- delete this object
{id: '3', name: 'Mikasa'}
];
Share
Improve this question
asked Aug 8, 2019 at 9:54
ArthurArthur
3,5067 gold badges35 silver badges64 bronze badges
4
|
4 Answers
Reset to default 7You can user filter
function, with a composed functions propEq & not
const result = filter(
compose(
not,
propEq('id', 2)
),
array,
)
console.log(result)
You can use both filter
or reject
:
R.reject(o => o.id === '2', arr);
R.filter(o => o.id !== '2', arr);
You can use reject
.
The reject() is a complement to the filter(). It excludes elements of a filterable for which the predicate returns true.
let res = R.reject(R.propEq('id', '2'))(arr);
// you could create a generic rejectWhere function
const rejectWhere = (arg, data) => R.reject(R.whereEq(arg), data);
const arr = [
{id: '1', name: 'Armin'},
{id: '2', name: 'Eren'}, // <- delete this object
{id: '3', name: 'Mikasa'}
];
console.log(
'result',
rejectWhere({ id: '2' }, arr),
);
// but also
// rejectWhere({ name: 'Eren' }, arr),
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>
R.filter(({ id }) => id !== '2', arr)
. For more info, refer: ramdajs.com/0.19.1/docs/#filter. Also a pointer, when you ask a question, please share your attempt in question. That makes a requirement, problem and we could help accordingly – Rajesh Commented Aug 8, 2019 at 10:04