/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/** *Allowstofindthelowestrankingindexinanindex *ofsuggestions,bycomparingittoanotherarrayof"mostrelevant"items *whichhasbeensortedbyrelevance. * *Exampleusage: *letsortedBrowsers=["firefox","safari","edge","chrome"]; *letmyBrowsers=["brave","chrome","firefox"]; *letbestBrowserIndex=findMostRelevantIndex(myBrowsers,sortedBrowsers); *// returns "2", the index of firefox in myBrowsers array * *@param{Array}items *ArrayofitemstocompareagainstsortedItems. *@param{Array}sortedItems *Arrayofsorteditemsthatsuggestionsareevaluatedagainst.Array *shouldbesortedbyrelevance,mostrelevantitemfirst. *@return{number}
*/ function findMostRelevantIndex(items, sortedItems) { if (!Array.isArray(items) || !Array.isArray(sortedItems)) { thrownew Error("Please provide valid items and sortedItems arrays.");
}
// If the items array is empty, no valid index can be found. if (!items.length) { return -1;
}
// Return 0 if no match was found in the suggestion list.
let bestIndex = 0;
let lowestIndex = Infinity;
items.forEach((item, i) => { const index = sortedItems.indexOf(item); if (index !== -1 && index <= lowestIndex) {
lowestIndex = index;
bestIndex = i;
}
});
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.