mardi 10 juillet 2018

looping through json data and match to an array

I am trying to match an array to an value of an object in my json, and then pull the other values in that object in a div, but it seems that it only matches to the last object of my json data. my array is countryPath which will have the the value de-de in the page, based on my URL. I want to show the details of the objects that have urlCode of de-de

also my code is javascript, I would like to convert it to Jquery if possible?

var data =
[
{
            "name":"Germany",
            "countryCode":"DE",
            "urlCode":"de-de",
            "standardPrice":"22",
            "standardFreeOver":"0",
            "standardTimes":"x"
        },
            {
            "name":"United Kingdom",
            "countryCode":"GB",
            "urlCode":"en-gb",
            "standardPrice":"30",
            "standardFreeOver":"x",
            "standardTimes":"x"
        }
];
data.forEach((o) =>{
  if(countryPath.includes(o.urlCode)){
document.getElementById('output').innerHTML = '<div class="' + o.countryCode + '"><div class="header"><h2>' + o.name + '</h2><img src="https://gepi.global-e.com/content/images/flags/' + o.imgCode + '.png"></div><h3>STANDARD DELIVERY</h3><br><p>Price: ' + o.standardPrice + ' or free for orders over ' + o.standardFreeOver +
'<br>Delivery Time:' + o.standardTimes + ' working days</p></div>';
} else {
   document.getElementById('output').innerHTML = '<div><p>please choose a country</p></div>'; 
}
});

Aucun commentaire:

Enregistrer un commentaire