简体   繁体   中英

Comparing two array objects

I am attempting to have a function compare objects in an array and see if they are the same

function alexareturn(alexaword, wordlist) {
  var rhyme = false
  console.log(alexaword + wordlist);
  for (var i = 0; i <= alexaword.length; i++) {
    for (var j = 0; j <= wordlist.length; j++) {
      if (alexaword[i].indexOf(wordlist[j]) > -1) {
        rhyme = true;
        break;
      }
    }
  }
  if (rhyme) {
    return true;
  } else {
    return false;
  }
}

My console is returning two proper arrays, however I am getting the error Cannot read indexOf (undefined). If i am getting console to return a proper array right before, how can one of them be undefined?

I would just like to call my function, and have it return true if there is a match of the two arrays, Thanks for your help!

Your outer for loop runs from 0 - alexaword.length (inclusive). In the last iteration alexaword[i] would be undefined.

for (var i = 0; i < alexaword.length; i++) would fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM