-1

So, the title pretty much says it all. I want to search a JSON, but try and not make it case sensitive. So here is my JSON file:

{ "Data" : [ {"Name": "Widget", "Price": "25.00", "Quantity": "5" }, {"Name": "Thing", "Price": "15.00", "Quantity": "5" }, {"Name": "Doodad", "Price": "5.00", "Quantity": "10" } ] } 

Unfortunately for me, I have not coded in a long time so basically I wrote the JavaScript part of my code in the HTML file also. It's a bit muddled together:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div> <form id="form1"> What product do you wish to find? <input name="name" type="text" size="20"> </form> <button id="fetchUserDataBtn">Cauta</button> </div> <hr> <div id="response"></div> <script> document.getElementById('fetchUserDataBtn').addEventListener('click', fetchUserData); function fetchUserData(){ fetch('Vlad3.json') .then(response => response.json()) .then(Data => { let output = "Product list" output += '<ul>'; var cauta1, cauta2; cauta1=document.getElementById("form1"); cauta2=cauta1.elements["name"].value; console.log(cauta2); var nr=0; Data.Data.forEach(function(Data) { if(Data.Name==cauta2) {nr++; output += "Product "+Data.Name; output+="<br>"; output+="Price "+Data.Price; output+="<br>"; output+="Quantity "+Data.Quantity; } }); if(nr==0) {output+="We are sorry, this is not available";} output += '</ul>' document.getElementById("response").innerHTML = output; }); } </script> </body> </html> 

Can I manage to add a function somewhere in here, so that when the user introduces "Thing" or "Widget" in the search area, it will find the product, even though it's not written exactly like in the JSON file?

1
  • Data.Name.toLowerCase() === cauta2.toLowerCase() Commented May 31, 2020 at 22:14

1 Answer 1

-1

Use .toLowerCase or .toUpperCase to do a case-insensitive compare.

if(Data.Name.toLowerCase()===cauta2.toLowerCase()) 
Sign up to request clarification or add additional context in comments.

1 Comment

I actually do not know why, but when I tried this earlier it did not work. I separately converted them and then compared them, but it did not work. Now I actually did as you said, and just transformed them simultaneously while also comparing them inside an IF statement, and it works perfectly. Weird. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.