0

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.

1
  • 1
    is this a special case where you cannot use normal javascript filter? Commented Jan 23, 2020 at 3:13

5 Answers 5

1

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

0

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>

Comments

0

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>

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.