简体   繁体   English

使用 Ramda 删除数组中的多个对象

[英]Remove multiple objects in an array using Ramda

is there a way in ramda to remove multiple object in ramda. ramda 中有没有办法删除 ramda 中的多个对象。

Here's my array这是我的阵列

const availableFeatures = [
  {
    id: 1,
    name: "TEST 1",
  },
  {
    id: 2,
    name: "TEST 2",
  },
  {
    id: 3,
    name: "TEST 3"
  }
]

I want to remove the object that contains id 1 and 2.我想删除包含 id 1 和 2 的对象。

I like using where to build predicates:我喜欢使用where来构建谓词:

 const x = reject(where({id: flip(includes)([1, 2])})) console.log(x(availableFeatures));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script> <script>const {reject, where, flip, includes} = R;</script> <script> const availableFeatures = [ { id: 1 , name: "TEST 1" }, { id: 2 , name: "TEST 2", }, { id: 3 , name: "TEST 3" } ]; </script>

Using R.reject() with R.propSatisfies() can allow you to remove all those objects in your array where the object's id is <= to 2 like so:使用R.reject()R.propSatisfies()可以让您删除数组中所有对象的id <=到 2 的对象,如下所示:

 const availableFeatures = [ { id: 1, name: "TEST 1", }, { id: 2, name: "TEST 2", }, { id: 3, name: "TEST 3" } ]; const res = R.reject(R.propSatisfies(R.gte(2), 'id'))(availableFeatures); console.log(res);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>

You can use reject() :您可以使用reject()

 const availableFeatures = [ { id: 1, name: "TEST 1", }, { id: 2, name: "TEST 2", }, { id: 3, name: "TEST 3" } ]; const result = R.reject(({id}) => id === 1 || id === 2, availableFeatures); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

You can use this solution您可以使用此解决方案

 const availableFeatures = [ { id: 1, name: "TEST 1", }, { id: 2, name: "TEST 2", }, { id: 3, name: "TEST 3" } ] const result = R.reject( R.anyPass([ R.propEq('id', 1), R.propEq('id', 2) ]) )(availableFeatures); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>

also something like this would do:也可以这样做:

 const blacklist = R.propSatisfies( R.includes(R.__, [1, 2]), 'id', ); const fn = R.reject(blacklist); // ---- const data = [ { id: 1, name: "TEST 1", }, { id: 2, name: "TEST 2", }, { id: 3, name: "TEST 3" } ]; console.log(fn(data));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM