2016-05-18 6 views
-1

I Array.splice aus dem Array ein beliebiges Element entfernen könnenErstellen Sie eine Funktion, die ein Array nimmt und Zahlen ein neues Array mit, die ein Vielfaches von X entfernt

ich speziell @ ein Vielfaches von 3 Suche bin und 5 in dem Array sollte

Multiples von X entfernt werden

Nummer 3 und Nummer 5 Multiples Was das Verfahren i für diese schreiben müssen, ist. Ich brauche Vanille 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> 
+1

Sie benötigen ** iterate ** über das Array. Sie können das mit einem ['for' loop] tun (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for). Mit welchem ​​Teil hast du Probleme? Wir helfen Ihnen gerne, Ihren Code zu reparieren, aber wir schreiben den Code nicht für Sie. –

+0

Mögliches Duplikat von [Ein bestimmtes Element aus einem Array in JavaScript entfernen?] (Http://stackoverflow.com/questions/5767325/remove-a-particular-element-from-an-array-in-javascript) – AlwaysNull

+0

Sie können mach es auch mit [.filter] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) –

Antwort

1

Das klingt wie eine Hausaufgabe. Hier ist eine heimliche Antwort, die nicht auf der integrierten Methode Array.prototype.filter beruht. Diese einfache rekursive Definition verwendet konstante Zeit und Raum.

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] 

Dies verwendet Array.prototype.filter und wahrscheinlich nicht, dass du irgendetwas lehrt.

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

Diese Kombinationen von einfachen Funktionen nutzt Ihr Ziel zu erreichen. Der Vorteil dieses Ansatzes besteht darin, dass jede Funktion eine einzige, einfache Sache ausführt und jede Funktion in hohem Maße wiederverwendbar ist.

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

Ok, jetzt sehen wir, dass alle Funktionen in Harmonie zusammenarbeiten!

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

Prob eine gute Verwendung für Filter:

Working Example

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

Oder mit einer for-Schleife:

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

Beschreibung

ein Komma getrennte Zeichenfolge gegeben, wenn multi Ziffern oder eine Zeichenfolge mit einer einzelnen mehrstelligen Zahl, können Sie diese Regex verwenden, um das führende Komma und die durch 3 oder 5 mit null teilbaren Zahlen zu ersetzen.

(?:,|^)((?:[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)

eine ausführlichere Erklärung dieses Ausdrucks finden Sie hier: siehe auch link

Ersetzen durch: nichts

Regular expression visualization

Beispiel

Live Demo

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

Beispiel String

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

Nach dem Austausch

1,2,16,23 

Erklärung

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 
---------------------------------------------------------------------- 
+0

Nun, das ist sicherlich eine * andere * Lösung ... Ich mag andere Möglichkeiten, um Probleme zu lösen, aber ich fürchte, das ist ziemlich unhaltbar. Es ist fast unmöglich zu verifizieren, dass dies für alle natürlichen Zahlen korrekt funktioniert. Ich nehme an, die Lösung für 5 wäre ein wenig einfacher tho, '/ \ d * [05] /'. – naomik

+0

Oh, ich stimme völlig zu, das ist nicht leicht zu pflegen, aber die OP fragte nach einem regulären Ausdruck, der Zahlen gleich durch 3 teilbar wäre, und zumindest in der Live-Demo funktioniert es wie erwartet und es ist die Theorie der Operation ziemlich gründlich überprüft. –

+1

Ich vermisste total den Teil, wo er/sie nach einem regulären Ausdruck fragte – naomik