使用Ramda从数组中删除数组?

时间:2021-05-14 21:22:51

Is there any shorthand option to remove array from array of arrays using Ramda library ?

是否有使用Ramda库从数组中删除数组的简写选项?

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]]

4 个解决方案

#1


2  

Use R.difference with R.flip:

使用R。与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>

#2


2  

A combination of R.reject, R.either and R.equals can achieve this.

R的组合。拒绝,R。要么和R。=可以实现这一目标。

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>

#3


0  

With vanilla JavaScript, you can make a copy of each of your two arrays, with .map() returning a string representation of your array elements.

使用普通的JavaScript,您可以复制两个数组中的每一个,并使用.map()返回数组元素的字符串表示形式。

Then loop over your removable items array and check with indexOf() over the existence of each item in your copied array.

然后遍历可移动项数组,并使用indexOf()检查复制数组中每个项的存在。

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);

#4


0  

This is probably the best answer

这可能是最好的答案

use R.reject and R.contains

使用R。拒绝和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)

#1


2  

Use R.difference with R.flip:

使用R。与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>

#2


2  

A combination of R.reject, R.either and R.equals can achieve this.

R的组合。拒绝,R。要么和R。=可以实现这一目标。

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>

#3


0  

With vanilla JavaScript, you can make a copy of each of your two arrays, with .map() returning a string representation of your array elements.

使用普通的JavaScript,您可以复制两个数组中的每一个,并使用.map()返回数组元素的字符串表示形式。

Then loop over your removable items array and check with indexOf() over the existence of each item in your copied array.

然后遍历可移动项数组,并使用indexOf()检查复制数组中每个项的存在。

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);

#4


0  

This is probably the best answer

这可能是最好的答案

use R.reject and R.contains

使用R。拒绝和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)