Если вы просто хотите сопоставить начало некоторого массива строк, вы можете использовать функциональную пару filter
и startsWith
.
let test = "a ";
const items = phrases.filter(phrase => phrase.startsWith(test));
ПРИМЕЧАНИЕ ЕСЛИ вам все равновсе строки в нижнем регистре используют .toLowerCase()
, чтобы они соответствовали "I" и "i";
let test = "a ";
const items = phrases.filter(phrase => phrase.toLowerCase().startsWith(test));
Примечание: я отредактировал этот живой пример, чтобы показывать также "содержит" через флажок, просто вслучай, который помогает проиллюстрировать альтернативу.
Вот живой пример:
let phrases = ["according to all known laws of aviation", "a blessing in disguise", "a bunch", "a dime a dozen", "beat around the bush", "better late than never", "bite the bullet", "break a leg", "call it a day", "cut somebody some slack", "cutting corners", "dead tired", "easy does it", "excuse me", "get it out of my system", "get it out of your system", "get out of hand", "get something out of my system", "get something out of your system", "get your act together", "give someone the benefit of the doubt", "go back to the drawing board", "good idea", "hang in there", "hit the nail on the head", "hit the sack", "how does that sound?", "i don't know", "i don't understand", "i love you", "i owe you one", "i'm fine", "i'm fine, thanks", "i'm really sorry", "i'm sorry", "it cost an arm and a leg", "it costs an arm and a leg", "it'll cost an arm and a leg", "it will cost an arm and a leg", "it's not rocket science", "i've never given it much thought", "kill two birds with one stone", "killing two birds with one stone", "let you off the hook", "let me off the hook", "look, it's", "make a long story short", "miss the boat", "never gonna give you up", "no pain, no gain ", "on the ball ", "once upon a time ", "pull your leg ", "pulling your leg ", "pull yourself together ", "proof of concept ", "so far so good ", "so much ", "speak of the devil ", "thanks so much ", "thank you so much ", "that's the last straw ", "the best of both worlds ", "this is a test", "time flies when you're having fun", "to be honest", "to get bent out of shape", "to kill a mockingbird", "to make matters worse", "under the weather", "watch out", "we'll cross that bridge when we come to it ", "whatever floats your boat", "whatever you say ", "wrap your head around something", "you can say that again", "your guess is as good as mine", "there's no such thing as a free lunch", "throw caution to the wind", "you can't have your cake and eat it too ", "have your cake and eat it too", "judge a book by its cover ", "book by its cover", "last straw", "shut up", "be quiet", "how are you?", "never gonna give you up", "water under the bridge", "let you down", "birds and the bees", "pair of trainers", "i'd really like", "i wouldn't mind", "i could do with"];
$('#mywords').on('input change', function(event) {
let grp = $("<div></div>");
let test = $(this).val();
$('#testing').html(test);
const items = phrases.filter(phrase => phrase.startsWith(test));
$.each(items, function(index, value) {
let rowItem = $("<div class='row-item'></div>").text(index + ". " + value);
grp.append(rowItem);
});
$('#results-list').html(grp.children());
});
function dochange(event) {
let grp = $("<div></div>");
let test = $('#mywords').val();
$('#testing').html(test);
let items = [];
if (!$('#mywords-contain').prop('checked')) {
items = phrases.filter(phrase => phrase.toLowerCase().startsWith(test));
} else {
items = phrases.filter(phrase => phrase.toLowerCase().includes(test));
}
$.each(items, function(index, value) {
let rowItem = $("<div class='row-item'></div>").text(index + ". " + value);
grp.append(rowItem);
});
$('#results-list').html(grp.children());
}
$('#mywords-contain').on('change', dochange);
$('#mywords').on('input change', dochange);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<label>Enter something:<input id="mywords" type="text"/></label><label>Check For contains:<input id="mywords-contain" type="checkbox"/></label><span id="testing">(empty)</span>
<div id="result-group">Result found:
<div id="results-list"></div>
</div>