Ramda: get objects from array by comparing with each item in another array

前端 未结 4 1075
-上瘾入骨i
-上瘾入骨i 2021-01-03 13:40

I\'ve an array like:

ids = [1,3,5];

and another array like:

items: [
{id: 1, name: \'a\'}, 
{id: 2, name: \'b\'}, 
{id: 3,          


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 14:14

    If you still want to do with Ramda:

    const ids = [1,3,5];
    
    const items = [
    {id: 1, name: 'a'}, 
    {id: 2, name: 'b'}, 
    {id: 3, name: 'c'}, 
    {id: 4, name: 'd'}, 
    {id: 5, name: 'e'}, 
    {id: 6, name: 'f'}
    ];
    
    console.log(
    
      R.filter(R.compose(R.flip(R.contains)(ids), R.prop('id')), items)
    
    );

提交回复
热议问题