3 回答
TA贡献1780条经验 获得超4个赞
您可以遍历uniquefromtwoarrays并检查数组中是否存在值
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贡献1815条经验 获得超10个赞
像这样的东西:
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贡献2080条经验 获得超4个赞
if ( secondDbArray.indexOf(uniquefromtwoarrays[i] === -1 )
// uniquefromtwoarrays[i] is related to the first array
else
// uniquefromtwoarrays[i] is related to the second array
添加回答
举报