Is there any shorthand option to remove array from array of arrays using Ramda library ?
Items to remove: [[1, 2], [a, b]]
Remove from: [[g, d], [5, 11], [1, 2], [43, 4], [a, b]]
Result: [[g, d], [5, 11], [43, 4]]
Is there any shorthand option to remove array from array of arrays using Ramda library ?
Items to remove: [[1, 2], [a, b]]
Remove from: [[g, d], [5, 11], [1, 2], [43, 4], [a, b]]
Result: [[g, d], [5, 11], [43, 4]]
Use R.difference with R.flip:
const data = [['g', 'd'], [5, 11], [1, 2], [43, 4], ['a', 'b']]
const itemsToRemove = [[1, 2], ['a', 'b']]
const fn = R.flip(R.difference)(itemsToRemove);
console.log(fn(data))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
This is probably the best answer
use R.reject and R.contains
var Itemstoremove = [[1, 2], [a, b]]
var Removefrom = [[g, d], [5, 11], [1, 2], [43, 4], [a, b]]
var Result = R.reject(R.contains(R.__, Itemstoremove), Removefrom)
console.log(Result)
A combination of R.reject, R.either and R.equals can achieve this.
const data = [['g', 'd'], [5, 11], [1, 2], [43, 4], ['a', 'b']]
const fn = R.reject(R.either(R.equals(['a', 'b']), R.equals([1, 2])))
console.log(fn(data))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
R.contains(R.__, [['a', 'b'], [1, 2]]) as a predicate makes it much easier to generalize. Or of course R.flip(R.contains). Regardless, the flip(difference) answer seems cleaner anyway.With vanilla JavaScript, you can make a copy of each of your two arrays, with .map() returning a string representation of your array elements.
Then loop over your removable items array and check with indexOf() over the existence of each item in your copied array.
This is how should be your code:
var arr = [["g", "d"], [5, 11], [1, 2], [43, 4], ["a", "b"]];
var strArr = arr.map(function(a){
return a.join(",");
});
var toRemove = [[1, 2], ["a", "b"]];
var strToRemove = toRemove.map(function(el){
return el.join(",");
});
strToRemove.forEach(function(a){
if(strArr.indexOf(a)>-1){
arr.splice(strArr.indexOf(a), 1);
strArr.splice(strArr.indexOf(a), 1);
}
});
Demo:
var arr = [
["g", "d"],
[5, 11],
[1, 2],
[43, 4],
["a", "b"]
];
var strArr = arr.map(function(a) {
return a.join(",");
});
var toRemove = [
[1, 2],
["a", "b"]
];
var strToRemove = toRemove.map(function(el) {
return el.join(",");
});
strToRemove.forEach(function(a) {
if (strArr.indexOf(a) > -1) {
arr.splice(strArr.indexOf(a), 1);
strArr.splice(strArr.indexOf(a), 1);
}
});
console.log(arr);
itemsToRemove contained ["43,4"], for instance, this would remove [43, 4]. Maybe that's not a concern for the expected data, but it makes it harder to generalize. The other two answers don't share this problem.[43, 4] thing, we could fix it, the purpose of my answer, is to use only vanilla JS to do it.["43,4"] or not removing [{a:1}, {b:2}] when the search list has [{a:1}, {b:3}]. toString can be a very blunt instrument.string for comparison may lead to some problems, but I gave this quick solution so it fits the OP requirements as they are :)