J'ai deux types de tableaux différents, et j'ai choisi des valeurs aléatoires dans deux tableaux différents et stockés dans un autre tableau, comment puis-je trouver quelles valeurs sont liées à quel tableau
exemple .... p >
var firstDbArray=[1,2,7,9,3,4,0,6,5,10] var secondDbArray=[1,2,9,7,0,4,8,11,15,30,10,12] var uniquefromtwoarrays= []; for (var i = 0; i < this.firstDbArray.length; i++) { if (this.secondDbArray.indexOf(this.firstDbArray[i]) === -1) { uniquefromtwoarrays.push(this.firstDbArray[i]); } } for (i = 0; i < this.secondDbArray.length; i++) { if (this.firstDbArray.indexOf(this.secondDbArray[i]) === -1) { uniquefromtwoarrays.push(this.secondDbArray[i]); } }
la sortie de uniquefromtwoarrays est ----> [3,6,5,8,11,15,30,12]
ma question est - -> comment puis-je trouver quelle valeur à partir de quel tableau dans cette sortie.
4 Réponses :
if ( secondDbArray.indexOf(uniquefromtwoarrays[i] === -1 ) // uniquefromtwoarrays[i] is related to the first array else // uniquefromtwoarrays[i] is related to the second array
Quelque chose comme ceci:
var firstDbArray=[1,2,7,9,3,4,0,6,5,10] var secondDbArray=[1,2,9,7,0,4,8,11,15,30,10,12] var uniquefromtwoarrays= []; //create a new array were will be store the origin of the value var uniqueArrayOrigin = []; for (var i = 0; i < this.firstDbArray.length; i++) { if (this.secondDbArray.indexOf(this.firstDbArray[i]) === -1) { //each time you push a new value to the array of unique values, //push to the new array of origins the value origins uniquefromtwoarrays.push(this.firstDbArray[i]); uniqueArrayOrigin.push(0); } } for (i = 0; i < this.secondDbArray.length; i++) { if (this.firstDbArray.indexOf(this.secondDbArray[i]) === -1) { uniquefromtwoarrays.push(this.secondDbArray[i]); uniqueArrayOrigin.push(1); } } for(i = 0; i<uniquefromtwoarrays.length; i++) { //Then, you can print the origin value by access to the same position as the unique values array (for this case 0 is for the first array and 1 is for the second array console.log(`origin: ${uniqueArrayOrigin[i]}; value: ${uniquefromtwoarrays[i]}`); }
Vous pouvez parcourir uniquefromtwoarrays
et vérifier l'existence de valeur dans les tableaux
var firstDbArray=[1,2,7,9,3,4,0,6,5,10] var secondDbArray=[1,2,9,7,0,4,8,11,15,30,10,12] var uniquefromtwoarrays= []; for (var i = 0; i < this.firstDbArray.length; i++) { if (this.secondDbArray.indexOf(this.firstDbArray[i]) === -1) { uniquefromtwoarrays.push(this.firstDbArray[i]); } } for (i = 0; i < this.secondDbArray.length; i++) { if (this.firstDbArray.indexOf(this.secondDbArray[i]) === -1) { uniquefromtwoarrays.push(this.secondDbArray[i]); } } let object = {firstDbArray,secondDbArray} uniquefromtwoarrays.forEach(value=>{ let includeIn = Object.entries(object).filter(([key,array])=>array.includes(value)).map(([key])=> key) console.log(`${value} is from in ${includeIn.join(', ')}`) })
Si vous souhaitez simplement voir l'origine, vous devez conserver une variable supplémentaire pour garder une trace de l'origine, quelque chose de similaire à Réponse de Ricardo
Je pense que ce petit extrait de code vous aidera ...
if ( secondDbArray.indexOf(uniquefromtwoarrays[i] === -1 ) // uniquefromtwoarrays[i] related to the first array else // uniquefromtwoarrays[i] related to the second array
dans le cas où une valeur est commune dans les deux tableaux, quelle sera la sortie souhaitée? vous voulez montrer l'origine de la valeur ou l'existence de la valeur?