Sydney Loteria
Sydney Loteria

Reputation: 10461

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.

Upvotes: 0

Views: 867

Answers (5)

Hitmands
Hitmands

Reputation: 14179

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>

Upvotes: 0

customcommander
customcommander

Reputation: 18901

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>

Upvotes: 1

Sachintha Nayanajith
Sachintha Nayanajith

Reputation: 721

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>

Upvotes: 0

Robby Cornelissen
Robby Cornelissen

Reputation: 97152

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>

Upvotes: 0

Nick Parsons
Nick Parsons

Reputation: 50759

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>

Upvotes: 0

Related Questions