I have two arrays, one with alphabetical letters and another array with words. I need to find all the letters that dont have a word in other array which starts from that letter. For example:
arr1 = [apple, bee, mom, dog] arr2 = [a,b,c,d]
I need a function which finds letter c from arr2, because it does not have any word starting from this letter.
My app has buttons with letters, and each letter has some content starting from that letter. I want to highlight the letters that have no content in there.
This is what I tried to do, and it works with === operator, but doesnt work with !=.
for(let i = 0; i < searchItemsTitle.length; i ) {
for(let e = 0; e < letterButtons.length; e ) {
if(searchItemsTitle[i].innerText.charAt(0) === letterButtons[e].innerText.charAt(0)) {
letterButtons[e].classList.add("a-z-grey-letter");
console.log(letterButtons[e].innerText)
}
}
}
CodePudding user response:
I think .some
may work for your issue. It will return either True
or False
based on if it can find the letter.
CodePudding user response:
you can use :
array.filter
to get only element that have a first letter present in letterButtons array- use array.foreach to add class for all these elements
const letterButtons = ['a', 'b', 'c', 'd'];
const searchItemsTitle = [...document.querySelectorAll('.title')];
const filteredItem = searchItemsTitle.filter(title => letterButtons.some(button => button.charAt(0) === title.innerText.charAt(0)));
filteredItem.forEach(item => item.classList.add("a-z-grey-letter"));
.a-z-grey-letter:first-letter {
color: grey;
}
<div >apple</div>
<div >bee</div>
<div >mom</div>
<div >dog</div>