Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反转数组_Javascript_Jquery_Arrays - Fatal编程技术网

Javascript 反转数组

Javascript 反转数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我正在使用给定数组的本机.filter()方法筛选该数组: var a = [1,2,3,4,5]; var b = a.filter(function(v) { return v > 2; }); 这将创建新数组([3,4,5])。现在,我还想在另一个数组中使用过滤后的值。我最好的选择是什么?我应该吗 将删除的值推入同一筛选方法中的新数组 编写一个反转函数,并在过滤后应用它 要使用第一个选项,它可能会以以下方式结束: var b = a.filter(function(v

我正在使用给定数组的本机
.filter()
方法筛选该数组:

var a = [1,2,3,4,5];

var b = a.filter(function(v) {
    return v > 2;
});
这将创建新数组(
[3,4,5]
)。现在,我还想在另一个数组中使用过滤后的值。我最好的选择是什么?我应该吗

  • 将删除的值推入同一筛选方法中的新数组
  • 编写一个反转函数,并在过滤后应用它
要使用第一个选项,它可能会以以下方式结束:

var b = a.filter(function(v) {
    return v > 2 || !c.push(v);
});
我对这个解决方案的问题是,它有点混合了两种不同的东西,对于将来阅读代码的人来说可能非常混乱。作为替代,我可以称之为

c = invert(a,b);

function invert(source, compare) {
    return source.filter(filterPositives);

    function filterPositives(v) {
        return compare.indexOf(v) === -1;
    };
}
这有效吗?还是我能做得更好


还有其他(更优雅的)方法来解决这个问题吗?

我不认为对源数组进行两次检查是一个优雅的解决方案。但是,再次强调,给你自己的
过滤器添加副作用也不是什么好事

我将通过编写一个
filterSplit
函数来解决这个问题,类似于以下伪代码:

function filterSplit(
      Array source, Array positives, Array negatives, Function filterCb) 
{
  source.forEach(function(el) {
    if (filterCb(el)) {
      positives.push(el); 
    }
    else {
      negatives.push(el);
    }
  }
}
。。。或者,如果您希望返回数组

function anotherFilterSplit(Array source, Function filterCb) {
  var positives = [], negatives = [];
  // ... the same check and push as above ...
  return [positives, negatives];
}
var a=[1,2,3,4,5],b=[],c=[];
对于(var i=0,l=a.length,ai=a[i];i2)b.push(ai);
否则c.push(ai);
控制台日志(b,c);
// [3, 4, 5]   [1, 2]

@raina77ow的解决方案看起来不错。更糟糕的是,以下是中的一个实现:

以及编译后的JavaScript

function filterSplit(source, predicate, positives, negatives){
  positives == null && (positives = []);
  negatives == null && (negatives = []);
  source.forEach(function(it){
    return (predicate(it) ? positives : negatives).push(el);
  });
  return [positives, negatives];
}

--因此,您可以传递您的
正片
负片
,或者不传递它们,这样您将得到新的空数组。

因为Andrew D.已经写了一个很好的答案,我不确定是否应该写这个,但是:


这看起来是一个非常优雅的解决方案(尽管我认为不必传入两个空(或非空)数组更符合
过滤器的要求)。感谢您的回复。但我觉得我真的不能用这个。我需要在源数组上循环以实际创建负值。所以我会再次循环两次,不是吗?@AndreMeinhold你能用源数组的例子更新你的问题吗?预期的正反两面都是这样吗?@raina77ow:就这些了。我用一个条件过滤一个数组,这将创建一个新的过滤数组。因此,该过滤器的“其余部分”应该成为另一个新阵列。第一个循环(过滤器),第二个循环(查找rest和push)JavaScript什么时候有类型化参数了?我关心的一个问题(如上所述)是可读性和方便性。我认为这不会有多大帮助:)为什么?一个
用于
lop,一个
if
语句。所有其他都是速度优化,可以删除。现在看起来更好了。当我写那篇评论时,你把它写得一字一句。@Andre Meinhold,“一字一句”——对不起,这是我的风格,但这不会改变任何事情。
function filterSplit (source, predicate, positives = [], negatives = [])
  source.forEach -> (if predicate it then positives else negatives).push el
  return [positives, negatives]
function filterSplit(source, predicate, positives, negatives){
  positives == null && (positives = []);
  negatives == null && (negatives = []);
  source.forEach(function(it){
    return (predicate(it) ? positives : negatives).push(el);
  });
  return [positives, negatives];
}
Array.prototype.divide = function(fun, neg) {
    if (this == null) throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun != "function") throw new TypeError();

    if (!(neg instanceof Array)) {
        throw new TypeError();
    }

    var res = [];
    neg.splice(0, neg.length);
    var thisp = arguments[2];
    for (var i = 0; i < len; i++) {
        if (i in t) {
            var val = t[i];
            if (fun.call(thisp, val, i, t)) {
                res.push(val);
            }
            else {
                neg.push(val);
            }
        }
    }

    return res;
};
var negatives = [];
var positives = x.divide(function(elem) {
    /* whatever you want to check */
}, negatives);