1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   | <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8"> 
<title>...</title>
<style>
</style>
 
</head>
<body>
<pre id="avant"></pre>
<input type="text" placeholder="Type de produit" id="t" />
<input type="text" placeholder="Origine" id="o" />
<input type="button" value="blacklister" id="v" />
<input type="button" value="ajouter" id="safe" />
<pre id="apres"></pre>
 
<script>
 
// produits disponibles
const list = {
        "bonbon": {
                "code":1,"pays":[
                        "Chine","Vietnam","Paraguay"
                ]
        },
        "viande": {
                "code":4,"pays":[
                        "Angleterre","Pérou","Tunisie"
                ]
        }
}
 
// affichage tableau produits
document.getElementById("avant").textContent=JSON.stringify(list,null,2)
 
// bouton blacklist et fonction
document.getElementById("v").addEventListener("click",()=>
        black(document.getElementById("t").value,document.getElementById("o").value)
)
 
const black=((t,o)=>{
        if(list[t]){
                const ind=list[t].pays.indexOf(o);
                if(ind!=-1){
                        list[t].pays.splice(ind,1)
                }
        }
        document.getElementById("apres").textContent=JSON.stringify(list,null,2)
 
})
 
// bouton ajouter (déblacklister, par exemple)  et fonction
document.getElementById("safe").addEventListener("click",()=>
        white(document.getElementById("t").value,document.getElementById("o").value)
)
 
const white=((t,o)=>{
        if(list[t]){
                const ind=list[t].pays.indexOf(o);
                if(ind==-1){
                        list[t].pays.push(o)
                }
        }
        document.getElementById("apres").textContent=JSON.stringify(list,null,2)
 
})
 
</script>
</body>
</html> | 
Partager