I have an array mainTitle that I am attempting to check if the string exists in listOfTitles then append to #mainTitle. If not then append to #otherTitle. In order to achieve this, I am looping through the array listOfTitles in order to check if any of the strings match with the first index of mainTitle
My issue is that string appends for every time the check fails due to the length of the array I am looping through. In this example, "Online" should only appear once in blue text, and "Retail" and "Store" which doesn't exist in listOfTitles should only appear once in red text.
I am attempting to have the string append-only once. How can I achieve with when needing to increment through the entire array in order to check if there is a match?
My expected outcome is to have only 1 "Online", "Retail", and "Store" to append and not the length of the string.
const mainTitle = ["Online", "Retail", "Store"]
const listOfTitles = ["Test","Test2","Test3","Test4","Test5","Test6", "Online", "Test7"]
for (let i = 0; i < mainTitle.length; i++) {
checkMatch(mainTitle[i])
};
function checkMatch (title) {
for (let i = 0; i < listOfTitles.length; i++) {
if (title=== listOfTitles[i]) {
$("#mainTitle").append(title);
} else {
$("#otherTitle").append(title);
}
}
}
#mainTitle {
color:blue;
}
#otherTitle {
color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mainTitle"> </div>
<div id="otherTitle"> </div>