简体   繁体   English

找到多个数组的差异。 使用reduce

[英]finding the difference in multiple arrays. using reduce

I am trying to recreate the underscore js function difference using reduce. 我试图使用reduce重新创建下划线js函数差异。 Difference takes in multiple arrays and returns all values that are similiar to the first array. 差异采用多个数组并返回与第一个数组类似的所有值。 so [1,2],[2,3],[1,3] should spit out [1,2,2,1]. 所以[1,2],[2,3],[1,3]应该吐出[1,2,2,1]。

I was thinking of looping through each of my subarray(rest) and if the value of my sub elements has an index in my first array then I will push that element onto my accumulator value(which is an empty array). 我正在考虑循环遍历每个子数组(休息),如果我的子元素的值在我的第一个数组中有一个索引,那么我将把该元素推送到我的累加器值(这是一个空数组)。

Some reason I do not get my intended output where I was expecting [1,2,3,2,1,2]. 有些原因我没有达到预期的输出[1,2,3,2,1,2]。 instead I am getting [ [ 1, 2 ], [ 2, 4 ], [ 1, 2 ] ]. 相反,我得到[[1,2],[2,4],[1,2]]。 Can anyone help me with this rewrite using reduce. 任何人都可以使用reduce重写这个重写。 Thanks. 谢谢。

function difference(arrays){
var arrayed=Array.prototype.slice.call(arguments);
var first=arrayed[0];
var rest=arrayed.slice(1);

return reduce(first,function(acc,cur){
  forEach(rest,function(sub){
    if (sub.indexOf(cur)>-1){
      acc.push(sub);
    }});
    return acc;
  },[]);
}

console.log(difference([1,2,3],[2,4],[1,2],[4,5]));

i know the way im calling forEach is different but it is because my own version of forEach accepts two arguments. 我知道我调用forEach的方式不同但是因为我自己的forEach版本接受了两个参数。

Here's a solution : 这是一个解决方案:

function difference(arrays){
  var arrayed = Array.prototype.slice.call(arguments);
  var first = arrayed.splice(0,1)[0]; // edit, thanks robG
  var tmp = [];

  return arrayed.reduce(function(acc, arr) {
    return acc.concat(arr.filter(function(el) {
      return first.includes(el);
    }));
  }, first);
}

console.log(difference([1,2,3],[2,4],[1,2],[4,5]));

Use the javascript reduce method on the 'rest' array, makes the first array be the default value of the accumulator, filter the arrays with the first one and concat the result to your accumulator. 在'rest'数组上使用javascript reduce方法,使第一个数组成为累加器的默认值,使用第一个数组过滤数组,并将结果连接到累加器。

Hope it helps, 希望能帮助到你,
best regards 最好的祝福

Could try this 可以试试这个

function difference(arrays){
  var arrayed=Array.prototype.slice.call(arguments);
  var first=arrayed[0];
  var rest=arrayed.slice(1);

  return first.concat(rest.reduce(function(acc, cur, curIndex, array){
    first.forEach(function(eachEle){
      if(cur.indexOf(eachEle) > -1 )
        acc.push(cur[cur.indexOf(eachEle)]);
    });
    return acc;
  }, []));
}

console.log(difference([1,2,3],[2,4],[1,2],[4,5]));

As for your previous question , you need to call array methods on arrays, not pass them as parameters so not 至于你上一个问题 ,你需要在数组上调用数组方法,而不是将它们作为参数传递给所以不是

return reduce(first,function(acc,cur){...})

but

return first.reduce(function(acc,cur){...});

In your function: 在你的功能:

var first=arrayed[0];
var rest=arrayed.slice(1);

can be replaced with one step using splice: 可以使用拼接一步替换:

var first = arrayed.splice(0, 1)

and use arrayed instead of rest . 并使用arrayed而不是rest But there's no need for that anyway. 但无论如何也没有必要。 If no accumulator is provided, then the first value is used, so (with some renaming of variables): 如果没有提供累加器,则使用第一个值,因此(对某些变量进行重命名):

 function difference(){ var args = Array.prototype.slice.call(arguments); return args.reduce(function(acc, cur){ cur.forEach(function(value){ if (acc.includes(value)){ acc.push(value); } }); return acc; }); } console.log(difference([1,2,3],[2,4],[1,2],[4,5])); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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