3 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以遍歷uniquefromtwoarrays并檢查數(shù)組中是否存在值
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(', ')}`)
})

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
像這樣的東西:
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]}`);
}

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
if ( secondDbArray.indexOf(uniquefromtwoarrays[i] === -1 )
// uniquefromtwoarrays[i] is related to the first array
else
// uniquefromtwoarrays[i] is related to the second array
添加回答
舉報(bào)