2016-06-30 6 views
0

Ich habe zwei Arrays. ein Array Sätze wie so:Javascript - Vergleichen Teilstring von Array 1 mit Strings in Array 2> Verwerfen, wenn mehrere Instanzen

var sentences = [ 
"Bob goes to the zoo", 
"Jim goes to the airport", 
"Jenny and Bob go to the beach", 
"Jenny goes to the cafe" 
] 

und die andere Namen wie so:

var names = [ 
"Bob", 
"Jim", 
"Jenny" 
] 

Was ich aus tun wollen in ein neues Array gesetzt wird, sondern nur die Zeichenfolgen, die nur eine Instanz haben aus dem Namen-Array. (So ​​in diesem Beispiel sollte die Zeichenfolge „Jenny und Bob an den Strand gehen“ auslassen

Beispiel Ausgabe:.

var output = [ 
"Bob goes to the zoo", 
"Jim goes to the airport", 
"Jenny goes to the cafe" 
] 

Ich weiß, wie ein Array-Element zu verwerfen, wenn nur eine Instanz vorhanden ist, aber nicht ganz sicher, wie etwa die Überprüfung für mehrere Instanzen gehen wie hier erforderlich:/

Antwort

5

Verwenden Array#filter Methode

var sentences = [ 
 
    "Bob goes to the zoo", 
 
    "Jim goes to the airport", 
 
    "Jenny and Bob go to the beach", 
 
    "Jenny goes to the cafe" 
 
    ], 
 
    names = [ 
 
    "Bob", 
 
    "Jim", 
 
    "Jenny" 
 
    ]; 
 

 

 
var res = sentences.filter(function(v) { // iterate over sentence array to filter 
 
    return names.filter(function(v1) { // iterate over names array and filter 
 
    return v.indexOf(v1) > -1; // get elements which contains in the sentence 
 
    }).length == 1; // filter based on the filtered names array length 
 
}); 
 

 
console.log(res);


Mit ES6 arrow function

var sentences = [ 
 
    "Bob goes to the zoo", 
 
    "Jim goes to the airport", 
 
    "Jenny and Bob go to the beach", 
 
    "Jenny goes to the cafe" 
 
    ], 
 
    names = [ 
 
    "Bob", 
 
    "Jim", 
 
    "Jenny" 
 
    ]; 
 

 

 
var res = sentences.filter(v => names.filter(v1 => v.indexOf(v1) > -1).length === 1); 
 

 
console.log(res);


UPDATE: Im Fall, wenn Sie das gesamte Wort übereinstimmen soll, dann kann es durch eine fixiert werden einfache Variante mit h Elle von String#split Methode.

var sentences = [ 
 
    "Bob goes to the zoo", 
 
    "Jim goes to the airport", 
 
    "Jenny and Bob go to the beach", 
 
    "Jenny goes to the cafe", 
 
    "Patrick says hello to Patricia" 
 
    ], 
 
    names = [ 
 
    "Bob", 
 
    "Jim", 
 
    "Jenny", 
 
    "Pat", 
 
    "Patricia" 
 
    ]; 
 

 

 
var res = sentences.filter(function(v) { 
 
    return names.filter(function(v1) { 
 
    // split based on space for exact word match 
 
    return v.split(/\s+/).indexOf(v1) > -1; 
 
    }).length == 1; 
 
}); 
 

 
console.log(res);

+0

Es ist ein potenzielles Problem mit dieser Antwort, wenn es ein Satz ist "' Patrick sagt Hallo zu Patricia'", und zwei weitere Namen "'" Pat " "Patricia"'". Was passiert ist, dass "Patrick Patrick zu Patricia grüßt" würde falsch ausgeschlossen werden. – kazenorin

+0

@kazenorin: einfache Variation kann es funktionieren 'v.split (/ \ s + /) indexOf (v1)' –

+1

Wow! Vielen Dank dafür !! Vor allem, um deinen Code zu kommentieren (ich finde es ein bisschen schwierig, mit Operationen über Arrays hinweg zu arbeiten, so dass deine Kommentare mir definitiv helfen, zu verstehen und zu lernen). Auch danke für das ES6-Beispiel, ich versuche allmählich, in den Griff zu kommen, es zu verwenden :) – AmyH

0

Kann Verwendung von Filter, Split machen und

var sentences = [ 
 
"Bob goes to the zoo", 
 
"Jim goes to the airport", 
 
"Jenny and Bob go to the beach", 
 
"Jenny goes to the cafe" 
 
]; 
 

 
var names = [ 
 
"Bob", 
 
"Jim", 
 
"Jenny" 
 
]; 
 

 
// Filter out the sentences 
 
var output = sentences.filter(function(sentence){ 
 

 
    // For each sentence, find number of times of appearance for each name 
 
    return sentence.split(" ").reduce(function(count, word){ 
 
    if(names.indexOf(word) >= 0) { 
 
     count += 1; 
 
    } 
 
    return count; 
 
    }, 0) === 1; 
 
}); 
 

 
console.log(output);

0

Sie können for Schleife, Array.prototype.join(), String.prototype.match()

0 verwenden reduzieren

var sentences = [ 
 
"Bob goes to the zoo", 
 
"Bob goes to the oz", 
 
"Jim goes to the airport", 
 
"Jim goes to the porch", 
 
"Jenny and Bob go to the beach", 
 
"Jenny goes to the cafe" 
 
] 
 

 
var names = [ 
 
"Bob", 
 
"Jim", 
 
"Jenny" 
 
]; 
 

 
for (var i = 0, s = sentences.join("\n") + "\n", res = []; 
 
    i < names.length; i++) { 
 
    res.push(s.match(new RegExp(".*(" + names[i] + ").*(?=\n)"))[0]) 
 
}; 
 

 
console.log(res);

Verwandte Themen