如何在随机存储到另一个数组后找到与哪个数组相关的值(例如:数组a,数组b)

我有两种不同的数组,我从两个不同的数组中选择了一些随机值并存储在另一个数组中,如何找到与哪个数组相关的值

例....

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]);
    }
  }

uniquefromtwoarrays的输出是----> [3,6,5,8,11,15,30,12]

我的问题是---->如何在此输出中找到哪个数组的值。


qq_遁去的一_1
浏览 439回答 3
3回答

Helenr

您可以遍历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(', ')}`)})

动漫人物

像这样的东西: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 valuevar uniqueArrayOrigin = [];&nbsp; for (var i = 0; i < this.firstDbArray.length; i++) {&nbsp; &nbsp; if (this.secondDbArray.indexOf(this.firstDbArray[i]) === -1) {&nbsp; &nbsp; &nbsp; //each time you push a new value to the array of unique values,&nbsp; &nbsp; &nbsp; //push to the new array of origins the value origins&nbsp; &nbsp; &nbsp; uniquefromtwoarrays.push(this.firstDbArray[i]);&nbsp; &nbsp; &nbsp; uniqueArrayOrigin.push(0);&nbsp; &nbsp; }&nbsp; }&nbsp; for (i = 0; i < this.secondDbArray.length; i++) {&nbsp; &nbsp; if (this.firstDbArray.indexOf(this.secondDbArray[i]) === -1) {&nbsp; &nbsp; &nbsp; uniquefromtwoarrays.push(this.secondDbArray[i]);&nbsp; &nbsp; &nbsp; uniqueArrayOrigin.push(1);&nbsp; &nbsp; }&nbsp; }&nbsp;&nbsp;for(i = 0; i<uniquefromtwoarrays.length; i++) {&nbsp;//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&nbsp;console.log(`origin: ${uniqueArrayOrigin[i]}; value:&nbsp;&nbsp;${uniquefromtwoarrays[i]}`);}

犯罪嫌疑人X

if ( secondDbArray.indexOf(uniquefromtwoarrays[i] === -1 )&nbsp;&nbsp;// uniquefromtwoarrays[i] is related to the first arrayelse&nbsp;// uniquefromtwoarrays[i] is related to the second array
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript