jquery – How can i get json key and value from autocomplete?

I want to get the key by searching for the value.

The resulting key will be used to use the post method in php language.

my json file is:

[
  {
    "1": "تهران (tehran)"
  },
  {
    "2": "اصفهان (isfahan)"
  },
  {
    "3": "ارومیه (urimiyeh)"
  },
  {
    "4": "اراک (arak)"
  },
  {
    "5": "اهواز (ahwaz)"
  },
  {
    "6": "تبریز (tabriz)"
  }
]

and script is:

 jQuery(document).ready(function(){ 
    var arrayReturn = [];
    jQuery.ajax({
        url: "cities.json",
        async: true,
        dataType: 'json',
        success: function (data) {
            for (var i = 0, len = data.length; i < len; i++) {
                
                arrayReturn.push({'value' : data[i].key});
            }


            //send parse data to autocomplete function
            loadSuggestions(arrayReturn);
            console.log(countries);         console.log(arrayReturn);
        }
    });
    function loadSuggestions(options) {
        jQuery('#autocomplete').autocomplete({
            lookup: options,
            onSelect: function (suggestion) {
                jQuery('#selected_option').html(suggestion.value);
            }
        });
    }
  });

The resulting key will be used to use the post method in php.

Read more here: Source link