| 12
 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
 
 | var tabData = ['toto','tata', [['toto',[['toto','tata'], 'toto','tata', "toti", ['titi','tata'], "toti"]],['titi','tata']], "toti"],
	tabValues = ['toto','tata'];
 
/*
 * dvjhInArray cherche si une valeur contenue dans le
 * tableau arrayValues est présente dans le tableau arrayData.
 *
 * arrayValues doit être un tableau simple (1 seul niveau)
 * 
 * arrayData peut être un tableau à plusieurs niveaux, dans
 * ce cas la fonction récursive dvjhSimpleArray le transforme
 * en un tableau simple.
 *
 * On retourne un array qui contient tabR et tabS.
 *
 * tabR est un tableau simple qui contient les éléments
 * présents dans tabS.
 *
 * tabS est un tableau simple qui contient les données présentes
 * dans arrayData.
 *
 * On ne stocke pas l'index de l'élément puisque indexOf() utilisé
 * par inArray()arrête la recherche au premier élément trouvé et
 * l'index dans tabS n'a rien à voir avec celui dans arrayData
 */
function dvjhInArray(arrayValues, arrayData){
	var tabS = [], tabR = [];
 
	dvjhSimpleArray(tabS, arrayData);
 
	$.each(arrayValues, function(i, item){
		if ( $.inArray(item, tabS) > -1 ){
			tabR.push(item);
		}
	});
 
	function dvjhSimpleArray(r, array){				
		$.each(array, function(i, item){
			if ($.isArray(item)){
				dvjhSimpleArray(r, item);
			} else {
				r.push(item);
			}
		});
	}
 
	return [tabR, tabS];
}
 
console.log( tabData );
var results = dvjhInArray(tabValues, tabData)
console.log( results[0] );
console.log( results[1] );			
 
/*
 ['toto','tata', [['toto',[['toto','tata'], 'toto','tata', "toti", ['titi','tata'], "toti"]],['titi','tata']], "toti"]
 ["toto", "tata", "toto", "toto", "tata", "toto", "tata", "toti", "titi", "tata", "toti", "titi", "tata", "toti"]
*/ | 
Partager