There is a code that simply compares the value in a variable with the value in json, when it finds a match, displays the necessary information. But I don’t manage to implement a mechanism that would display a message that there are no search results.

Instead, the function displays that there are no results several thousand times. That is, every time a reconciliation occurs and there are no matches, json displays a message.

How can I solve the problem?

Here is my code:

function Zvalue(value_test) { $.each(allNam, function(key, val) { if (value_test == value['actNam']) { setTimeout(function() { gomasege(value['varNam']) }, 600); } else if (value_test !== value['actNam']) { console.log("нет совпадений") } }) } 

  • And what is the value [] variable? It is not declared anywhere in the code ... Or is it a typo in the example and should be val [] - Mike
  • @ Mike, typo thanks, corrected - Dementiy1999

1 answer 1

You can solve the problem using the flag:

  function Zvalue(value_test) { var anyMatches = false; $.each(allNam, function(key, val) { if (value_test == val['actNam']) { anyMatches = true; setTimeout(function() { gomasege(val['varNam']); }, 600); } }); if (!anyMatches) console.log("нет совпадений"); } 

Offtop
Small thing for holivar. If allNam is big enough, it's better to replace $.each with a for () loop, because jQuery $.each() is not well implemented.