最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete object by id from array Ramda - Stack Overflow

programmeradmin3浏览0评论

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
  • Have you tried anything? Its a fairly simple operation. – Rajesh Commented Aug 8, 2019 at 10:00
  • @Rajesh, sure, I've tried to use R.filter(), R.reject(), but I still new in Ramda – Arthur Commented Aug 8, 2019 at 10:03
  • Possible duplicate: stackoverflow.com/questions/29254470/… – Rajesh Commented Aug 8, 2019 at 10:03
  • Try 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
Add a comment  | 

4 Answers 4

Reset to default 7

You 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>

发布评论

评论列表(0)

  1. 暂无评论