javascript - Searching JSON keys by keywords -
i have json list this:
{ "stattrak™ ak-47 | fire serpent (field-tested)": 701, "stattrak™ ak-47 | fire serpent (minimal wear)": 1050, "stattrak™ ak-47 | fire serpent (well-worn)": 450.15, "stattrak™ ak-47 | fire serpent (factory new)": 3300, "ak-47 | fire serpent (field-tested)": 195.8, "stattrak™ ak-47 | fire serpent (battle-scarred)": 369.58, "ak-47 | fire serpent (factory new)": 750, }
if user types in box example: ak fire serpent
return ak fire serpents
here code have tried use:
var itemwanted = 'ak fire serpent'; var message = itemwanted.split(" "); for(var price in prices){ var splitfast = price.split(" "); if(price.tolowercase().includes(message[1].tolowercase())){ if(splitfast[1].tolowercase().includes(message[0].tolowercase())){ console.log(price + ' - ' + prices[price]); } } }
it splits user message , tries find it, try search name fire
right example, there ~30 items name fire
in it, added 2nd part of code checks weapon type, returns me stattrak
ones because checks 1
best way of doing because splitting doesn't seem work.
obviously, not know json file from, if have chance change json format bit without putting lot of effort in it, try built json structure bit "smarter".
for example:
{ "weapon type": { "stattrak™ ak-47" : { "fire serpent": { "(field-tested)": 1050, "(factory new)" : 50 } } "ak-47" : "fire serpent": { "(field-tested)": 1050, "(factory new)" : 50 } } }
it might run through json file lot smoother , in intuitive way.
Comments
Post a Comment