为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

qq_遁去的一_1 2019-04-18 15:15:57
我有两种不同的数组,我从两个不同的数组中选择了一些随机值并存储在另一个数组中,如何找到与哪个数组相关的值例....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]我的问题是---->如何在此输出中找到哪个数组的值。
查看完整描述

3 回答

?
Helenr

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(', ')}`)

})


查看完整回答
反对 回复 2019-05-17
?
动漫人物

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

}


查看完整回答
反对 回复 2019-05-17
?
犯罪嫌疑人X

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


查看完整回答
反对 回复 2019-05-17
  • 3 回答
  • 0 关注
  • 432 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信