-1

I am having an issue with a piece of javascript that's evaluating to true when I do not want it to do so. When indexOf evaluates rec.name - which equals "CSI" - it returns true and triggers the continue line because the finalArr array contains an element named "CSIQ Group". However, that's not what I want, as I only want it to evaluate to true if it finds an element in the array that is an exact match.

Here's the snippet of code:

if(finalArr.join().indexOf(rec.name.toString()) > -1){
            continue;
        }

What can I change to prevent "CSI" from triggering the continue line when "CSIQ Group" is already in finalArr? Thanks!

Kind Regards, Joseph

Joseph316
  • 69
  • 6
  • 1
    Don't use `indexOf` - because it finds the index of the first match it finds. If you want equivalency, then use the equivalence operator: `===` – Randy Casburn Feb 24 '21 at 05:37
  • Thank you for pointing that out. toString() was in the base code. I just tried removing it but the issue still persists, though. – Joseph316 Feb 24 '21 at 05:37
  • 1
    https://stackoverflow.com/questions/6298566/match-exact-string – abhay Feb 24 '21 at 05:39
  • let str = 'CSI Group'; if(str == 'CSI Group'){ console.log('yes') } – sonEtLumiere Feb 24 '21 at 05:40
  • 2
    Please show `finalArr` and the exact expected output as a [mcve]. If you're just searching an array, maybe `["bar", "foobar", "foob"].includes("foo")` => false. – ggorlen Feb 24 '21 at 05:40
  • Thank you abhay. I'm not sure how to use that content to iterate through all the items in the finalArr array to see if there are any exact matches. hmmm... – Joseph316 Feb 24 '21 at 05:41
  • 2
    Why are you joining the array? Are you trying to find when `CSI` equals something in `[C,S,I,something,something,...]`? or are you trying to find an element `['CSI', 'something', ...]` just trying to get a better answer for you. – Brett East Feb 24 '21 at 05:41
  • Thank you @BrettEast!, it looks like joining the array was the root cause. I took out that join and now the indexOf is evaluating as I desire on both partial (false) and whole match (true) scenarios. – Joseph316 Feb 24 '21 at 05:47
  • 1
    @Joseph316 no worries, defs check out my actual answer as I think using `includes` is perfect for your situation and is easy code to read. Also highly, highly recommend the MDN docs for arrays, they have taught me so much about using arrays over the years. – Brett East Feb 24 '21 at 05:49

2 Answers2

3

You could try using findIndexOf or in your case just find should do the trick, or even includes.

Something like this:

if(finalArr.includes(rec.name){
  continue;
}

includes is great for a simple string match. If you want greater matching then you can try find. find will let you compare each element in the array against a certain condition and you can perform multiple checks on it.

if(!!finalArr.find(element => (element.toLowerCase() === name.rec.toLowerCase()){
  continue;
}

I would, however, definitely recommend against converting your array to a string and trying to search it, especially for this case.

Brett East
  • 4,022
  • 2
  • 20
  • 31
  • 1
    Taking out the join was the key - that was the wrench in my gears. Thank you for the additional tips, too! – Joseph316 Feb 24 '21 at 05:59
0

You could use RegExp to match the string exactly to CSI:

const pattern = /^CSI$/ // ^ - start of the string, $ - end of the string
pattern.test(rec.name.toString()) // true if only "CSI", false otherwise

You could use this to amend your code and do what you need if word CSI is found.

Mantas Astra
  • 952
  • 6
  • 14