lundi 8 juin 2020

Sort Multiple Key Value pair in Json and get a random value from the sorted Key

I have a following values in JSON

[  
    {  
      "country":"First",
      "coupon":["1"]
    },
    {  
      "country":"First",
      "coupon":["10"]
    },
    {  
      "country":"First",
      "coupon":["12"]
    },
    {  
      "country":"Second",
      "coupon":"2"
    },
    {  
      "country":"third",
      "coupon":"3"
    },
    {  
      "country":"fourth",
      "coupon":"4"
    },
    {  
      "country":"fifth",
      "coupon":"5"
    }
  ]

I sorted out the duplicates in json and displayed on on dropdown

     var sortedCountries = [];
     if (sortedCountries.indexOf(value.country) == -1) {
$('#sel').append('<option value="' + value.coupon + '">' + value.country + '</option>');
          sortedCountries.push(value.country);
}

So when i select each option in dropdown the corresponding value i.e coupon appears. But i need to generate random values (coupons) when i select the dropdown with "country":"First", How to do that ?




Aucun commentaire:

Enregistrer un commentaire