2017-02-23 3 views
-1

Ich habe ein Array von Gegenständen:Suche nach einem Sub-Array in einem Array in JavaScript

arrObj : [{ id: 0, text: 'At', start: '15.000' }, 
     { id: 1, text: 'the', start: '15.492'}, 
     { id: 2, text: 'left', start: '15.984'}, 
     { id: 3, text: 'we', start: '16.476' }, 
     { id: 4, text: 'can', start: '16.967'}, 
     { id: 5, text: 'see...', start: '17.459' }, 
     { id: 6, text: 'At', start: '18.166'}, 
     { id: 7, text: 'the', start: '18.440' }] 

Ich habe für ein Array zu suchen und das Start- und End-Wort-IDs zurück. Zum Beispiel in diesem Fall:

["At", "the"] 

Ich muß zurückkehren [(0,1), (6,7)] ich derzeit für jede Schleife bin mit über die arrObj iterieren und sehen, ob das Spiel Worte . Ich habe auch indexOf versucht, indem ich die Objekte texte, aber es gibt den Char-Index nicht Array-Index zurück.

Aber das scheint nicht effizient. Wie kann ich so etwas effizient suchen?

+3

noch vage. Erkläre mehr! –

+0

Bitte lesen Sie [fragen]. Schlüsselbegriffe: "Suchen und forschen" und "Erkläre ... alle Schwierigkeiten, die dich daran gehindert haben, es selbst zu lösen". –

+0

Schauen Sie in ['indexOf'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) oder [' includes'] (https: // developer. mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes), wenn Sie auf neuere Browser abzielen. –

Antwort

1

könnten Sie reduzieren verwenden, um die Start- und Endpositionen in einem Array zu erhalten:

let arrObj = [{ id: 0, text: 'At', start: '15.000' }, 
 
     { id: 1, text: 'the', start: '15.492'}, 
 
     { id: 2, text: 'left', start: '15.984'}, 
 
     { id: 3, text: 'we', start: '16.476' }, 
 
     { id: 4, text: 'can', start: '16.967'}, 
 
     { id: 5, text: 'see...', start: '17.459' }, 
 
     { id: 6, text: 'At', start: '18.166'}, 
 
     { id: 7, text: 'the', start: '18.440' }]; 
 

 
let arr = ["At", "the"]; 
 
let res = arrObj.reduce((a, b, i) => { 
 
    let index = arr.indexOf(b.text); 
 
    if (index === 0) { 
 
    a.push(i); 
 
    } else if (index === 1) { 
 
    a.push([a.pop()].concat(i)); 
 
    } 
 
    return a; 
 
}, []); 
 

 
console.log(res);

Beachten Sie, dass dies nur funktioniert, wenn das Array mit den Suchbegriffe 2 Einträge enthält.

Wenn Sie mehr als zwei Suchbegriffe in der Anordnung brauchen, wird diese Arbeit:

let arrObj = [{ id: 0, text: 'At', start: '15.000' }, 
 
     { id: 1, text: 'the', start: '15.492'}, 
 
     { id: 2, text: 'left', start: '15.984'}, 
 
     { id: 3, text: 'we', start: '16.476' }, 
 
     { id: 4, text: 'can', start: '16.967'}, 
 
     { id: 5, text: 'see...', start: '17.459' }, 
 
     { id: 6, text: 'At', start: '18.166'}, 
 
     { id: 7, text: 'the', start: '18.440' }] 
 

 
let arr = ["At", "the", "left"]; 
 
let res = arrObj.reduce((a,b,i) => { 
 
\t let index = arr.indexOf(b.text); 
 
    if (index > -1) { 
 
    if (index % arr.length === 0) { 
 
    \t a.push(i); 
 
    } else { 
 
    \t let tmp = a.pop(); 
 
    a.push(tmp instanceof Array ? tmp.concat(i) : [tmp].concat(i)); 
 
    } 
 
    } 
 
    return a; 
 
}, []); 
 
console.log(res);

+0

Danke das funktioniert! – funkadelic

+0

Sollte die Ausgabe des zweiten Snippets nicht "[[0,1,2]]" statt "[[0,1,2], [6,7]]" lauten? –

+0

Ich glaube nicht, es scheint, als ob es das löst, was OP hinterher wollte.Es wird nicht angegeben, ob eine Teilübereinstimmung ausgeschlossen werden soll, und der zweite Teilabschnitt ist nur zur Vervollständigung da. Ich denke, der erste Teil ist besser für die Frage geeignet. @RickHitchcock – baao

1

Eine Lösung für jede Länge eines Such Array. Es speichert den Index des Sucharrays und erhöht es mit einer Übereinstimmung.

Es fügt nur ein Array mit den Indizes hinzu, wenn die Indizes für alle Elemente des Sucharrays gefunden werden.

var array = [{ id: 0, text: 'At', start: '15.000' }, { id: 1, text: 'the', start: '15.492' }, { id: 2, text: 'left', start: '15.984' }, { id: 3, text: 'we', start: '16.476' }, { id: 4, text: 'can', start: '16.967' }, { id: 5, text: 'see...', start: '17.459' }, { id: 6, text: 'At', start: '18.166' }, { id: 7, text: 'the', start: '18.440' }], 
 
    search = ["At", "the"], 
 
    result = array.reduce(function (i, t) { 
 
     return function (r, a, j) { 
 
      if (search[i] === a.text) { 
 
       t.push(j); 
 
       i++; 
 
       if (i === search.length) { 
 
        r.push(t); 
 
        t = []; 
 
        i = 0; 
 
       }; 
 
      } 
 
      return r; 
 
     }; 
 
    }(0, []), []); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Verwandte Themen