How to filter array values greater than x

前端 未结 4 554
-上瘾入骨i
-上瘾入骨i 2021-01-20 11:54

I\'ve been looking around on the internet and I cant find any posts that cover how to fix this even though I am certain it is a very simple fix.

Basically I have an

4条回答
  •  执笔经年
    2021-01-20 12:23

    function predicate(x) { return x > 10 }
    var output = input.filter(predicate);
    input = input.filter(function(x) { return !predicate(x) })
    

    Looks even cleaner with ES6 arrow functions:

    var predicate = (x) => x > 10;
    var output = input.filter(predicate);
    input = input.filter(x => !predicate(x));
    

提交回复
热议问题