Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to search for items in array properties?

I am trying to make a simple website(without CSS3) to search for items in an array. My way of accomplishing this goal is to search in the ‘title’ or ‘desc’ properties of an item in the array. My expected result is to get titleOfItem + ‘ fizz’ in the console if the title includes the keyword from the input. Instead, I get the following error:
console image & error
EDIT: I fixed this by adding a condition instead of a number in the for loop.

Here is my HTML5 code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>replit</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <input id="keywordText" type="text">
    <button id="submit" onclick="search()">Search</button>
    <script src="script.js"></script>
  </body>
</html>

and Here is my JS code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const items = {
  john:{title:'john', desc:"doe", elem:document.getElementById('john')},
  jane:{title:'jane', desc:"doe", elem:document.getElementById('jane')}
}

let allItems = []
for (var key in items) {
  allItems.push(items[key])
}



function search() {
  let keyword = document.getElementById('keywordText').value;
  for (let count = 0; allItems.length; count++) {
    let titleOfItem = allItems[count].title
    if (titleOfItem.includes(keyword)) {
      console.log(titleOfItem + ' fizz')
    } else {
      console.log(titleOfItem + ' buzz')
    }
  }
}

Is there something that I am doing wrong in this code? Also, for organization purposes, is there some way to get this information straight from the first array?

Thank you for your help!

>Solution :

for (let count = 0; count < allItems.length; count++) {

}

You are getting the error because you did not specify how long the count value will continue in the for loop. You can fix the problem by updating the code like this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading