创建一个函数,该函数接受一个数组并返回一个新数组,其中数字为X的倍数

时间:2016-05-18 23:37:24

标签: javascript arrays frontend

我可以使用array.splice从数组中删除任何元素

我特意在数组

中查看@ 3和5的倍数

应删除X的倍数

3号和5号倍数 我需要为此编写什么方法。我需要使用Vanilla Javascipt

                var myArray = [1,2,3,5,9,15,16,21,23,25];

            function removeMultiples(ary, num) {



            }

            //removeMultiples(myArray, 3) => [1,2,5,16,23,25]
            //removeMultiples(myArray, 5) => [1,2,3,9,16,21,23]
          </script>

3 个答案:

答案 0 :(得分:2)

很好地利用过滤器:

Working Example

function removeMultiples(arr, mul) {
  return arr.filter(function(el) {
    return el % mul !== 0;
  })
}

或者使用for循环:

function remove(arr, mul) {
  var result = [];
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] % mul) {
      result.push(arr[i]);
    }
  }
  return result;
}

答案 1 :(得分:2)

描述

如果使用逗号分隔的字符串(如果是多位数字或带有单个多位数的字符串),则可以使用此正则表达式替换前导逗号,并且可以被35整除的数字与null。

(?:,|^)((?:[0369]|[258][0369]*[147]|[147](?:[0369]|[147][0369]*[258])*[258]|[258][0369]*[258](?:[0369]|[147][0369]*[258])*[258]|[147](?:[0369]|[147][0369]*[258])*[147][0369]*[147]|[258][0369]*[258](?:[0369]|[147][0369]*[258])*[147][0369]*[147])*|[0-9]*[50])(?=,|\Z)

可以在此处找到有关此表达式的更详细说明:另请参阅link

替换为: 没有

Regular expression visualization

实施例

现场演示

https://regex101.com/r/iG9lP4/1

示例字符串

1,2,3,5,9,15,16,21,23,25

更换后

1,2,16,23

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    ,                        ','
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ^                        the beginning of a "line"
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
        [0369]*                  any character of: '0', '3', '6', '9'
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
        [0369]*                  any character of: '0', '3', '6', '9'
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
        [0369]*                  any character of: '0', '3', '6', '9'
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
        [0369]*                  any character of: '0', '3', '6', '9'
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [0-9]*                   any character of: '0' to '9' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [50]                     any character of: '5', '0'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    ,                        ','
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \Z                       before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------

答案 2 :(得分:1)

这听起来像是家庭作业。这是一个不依赖于Array.prototype.filter内置方法的家庭作业答案。这个简单的递归定义使用恒定的时间和空间。

const removeMultiples = (n, xs)=> {
  let loop = (ys, xs)=> {
    if (xs.length === 0)
      return ys;
    else if (xs[0] % n === 0)
      return loop(ys, xs.slice(1));
    else
      return loop(ys.concat([xs[0]]), xs.slice(1));
  };
  return loop([], xs);
};

removeMultiples(3, [0,1,2,3,4,5,6,7,8,9]);
//=> [1,2,4,5,7,8]

这使用了Array.prototype.filter,可能并没有教会你什么。

myArray.filter(x=> x % 3 !== 0);
myArray.filter(x=> x % 5 !== 0);

这使用简单函数的组合来实现您的目标。这种方法的好处是每个函数都做一个简单的事情,每个函数都是高度可重用的。

// (b->c) -> (a->b) -> (a->c)
const comp = f=> g=> x=> f (g (x));

// Boolean -> Boolean
const not = x=> !x;

// Number -> Number -> Boolean
const isDivisibleBy = x=> y=> y % x === 0;

// Array -> Boolean
const isEmpty = xs=> xs.length === 0;

// [Value] -> Value
const first = xs=> xs[0];

// [Value] -> [Value]
const rest = xs=> xs.slice(1);

// [Value] -> [Value] -> [Value]
const concat = xs=> ys=> ys.concat(xs);

// Value -> [Value] -> [Value]
const append = x=> concat([x]);

// (a->b->a) -> a -> [b] -> a
const reduce = f=> y=> xs=>
  isEmpty(xs) ? y : reduce (f) (f (y) (first (xs))) (rest (xs));

// (Value->Boolean) -> [Value] -> [Value]
const filter = f=>
  reduce (ys=> x=> f (x) ? append (x) (ys) : ys) ([]);

// Number -> [Number] -> [Number]
const removeMultiples = x=>
  filter (comp (not) (isDivisibleBy(x)));

好的,现在让我们看看所有功能协调一致!

removeMultiples (3) ([1,2,3,5,9,15,16,21,23,25]);
//=> [ 1, 2, 5, 16, 23, 25 ]

removeMultiples (5) ([1,2,3,5,9,15,16,21,23,25]);
//=>  [ 1, 2, 3, 9, 16, 21, 23 ]