简体   繁体   中英

Remove multiple objects in an array using Ramda

is there a way in ramda to remove multiple object in 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.

I like using where to build predicates:

 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:

 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() :

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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