Script unique : Cr饌tion de la fonction array_unique()
Cr饌tion de la fonction array_unique() qui 駘imine les doublons d'un tableau
Partie I : Les bases de JavaScript
Edition 1 | Chapitre 7 : Les tableaux / Page 104
Edition 2 | Chapitre 7 : Les tableaux / Page 112
Edition 3 | Chapitre 7 : Les tableaux / Page 112
Exécution du script
Emulation de la console
Code source
<html><head> <title>Création de la fonction array_unique()</title></head><body><script>function arrayUnique(tableau) { var tmp=[]; tableau.forEach(function(e) { if (tmp.indexOf(e)==-1) { /* Si e n'est pas dans tmp */ tmp.push(e); } }); return tmp;}var doublons=["Jean", "Gérard", "Pierre", "Paul", "Jean", "Paul", "Jacques"];console.log(doublons.toString());console.log(arrayUnique(doublons).toString());</script></body></html>