Javascript 两个数组,长度相同。如果第一个数组值等于true,则从第二个数组获取值

Javascript 两个数组,长度相同。如果第一个数组值等于true,则从第二个数组获取值,javascript,Javascript,我有两个数组。它们总是一样长。如果userEmotions[index]值为true,我需要将userTimeData[index]值添加到变量中: var userEmotions = [false, true, false, true], userTimeData = [140, 320, 730, 50], userPercentage = 0; // e.g add 320 and 50 to userPercentage: // userPercentage == 3

我有两个数组。它们总是一样长。如果userEmotions[index]值为true,我需要将userTimeData[index]值添加到变量中:

var userEmotions = [false, true, false, true],
    userTimeData = [140, 320, 730, 50],
    userPercentage = 0;

// e.g add 320 and 50 to userPercentage:
// userPercentage == 370

如何实现这一点?

您可以使用for循环:

var userEmotions=[false,true,false], userTimeData=[140320730], 用户百分比=0; 对于i=0;i.长度;i++{//循环数组的长度 如果userEmotions[i]{//检查userEmotions是否为true userPercentage+=userTimeData[i];//如果是,则将userTimeData添加到percentage } }
console.loguserPercentage 可以使用for循环:

var userEmotions=[false,true,false], userTimeData=[140320730], 用户百分比=0; 对于i=0;i.长度;i++{//循环数组的长度 如果userEmotions[i]{//检查userEmotions是否为true userPercentage+=userTimeData[i];//如果是,则将userTimeData添加到percentage } }
console.loguserPercentage 为什么过度设计它,一个简单的for循环解决了你的问题-

var userEmotions=[false,true,false], userTimeData=[140320730], 用户百分比=0; 对于i=0;i.长度;i++{ 如果是[i]{ userPercentage+=userTimeData[i]; } }
console.loguserPercentage 为什么过度设计它,一个简单的for循环解决了你的问题-

var userEmotions=[false,true,false], userTimeData=[140320730], 用户百分比=0; 对于i=0;i.长度;i++{ 如果是[i]{ userPercentage+=userTimeData[i]; } } console.loguserPercentage 您可以使用第一个单值或零作为默认值

var userEmotions=[false,true,true], userTimeData=[140320730], userPercentage=userTimeData[userEmotions.indexOftrue]| | 0; console.loguserPercentage 您可以使用第一个单值或零作为默认值

var userEmotions=[false,true,true], userTimeData=[140320730], userPercentage=userTimeData[userEmotions.indexOftrue]| | 0; console.loguserPercentage 你能试试这个吗

for(let i = 0 ; i < userEmotions .length ; i++){
   if(userEmotions[i]){
       userPercentage = userTimeData[i]
   }
}
如果有帮助,请告诉我:

您能试试这个吗:

for(let i = 0 ; i < userEmotions .length ; i++){
   if(userEmotions[i]){
       userPercentage = userTimeData[i]
   }
}

如果对您有帮助,请告诉我:

OP需要其他方法,因此:

userEmotions=[false,true,false], userTimeData=[140320730], userPercentage=userEmotions .mapx=>x?1 : 0 .mapx,i=>x*userTimeData[i] .reducea,b=>a+b;
console.loguserPercentage OP需要替代方法,因此:

userEmotions=[false,true,false], userTimeData=[140320730], userPercentage=userEmotions .mapx=>x?1 : 0 .mapx,i=>x*userTimeData[i] .reducea,b=>a+b;
console.loguserPercentage;您只需要一个值还是一个值数组?你试过什么?一个简单的目标是什么?我如何实现这个目标你把课文中的第三句话变成代码。简单到:对于userEmotions中的索引{if userEmotions[index]{userPercentage+=userTimeData[index];}。不足以计算百分比,但您可以将其用作起点。userPercentage=userEmotions.reducesum,e,i=>sum+e&&userTimeData[i],0@NinaScholz一个值数组。您只需要一个值还是一个值数组?你试过什么?一个简单的目标是什么?我如何实现这个目标你把课文中的第三句话变成代码。简单到:对于userEmotions中的索引{if userEmotions[index]{userPercentage+=userTimeData[index];}。不足以计算百分比,但您可以将其用作起点。userPercentage=userEmotions.reducesum,e,i=>sum+e&&userTimeData[i],0@NinaScholz一个值数组。哦,我刚才演示了当trues的数目不是1时,您的代码没有给出正确的答案。@BuhBuh,用户想要得到一个值,正如文章标题所要求的那样,结果应该采用第一个真值的索引。UserMootations数组中的另一个true出现了什么问题?我现在看到您以不同的方式穿插了这个问题。我理解第一个是指第一个数组,你把它理解为第一个布尔值。也许你是对的。这个问题是不明确的。现在我再次阅读了这个问题,是的,它是不明确的,我指的是第一个数组。userPercentages.reducea,b=>a+b,0。出于好奇,过滤器中的u,part做什么?u只是一个变量,没有被使用。哦,我只是在演示当trues的数目不是一时,你的代码没有给出正确的答案。@BuhBuh,用户想要得到一个单一的值,并且第一个真值的索引应该作为结果,正如文章的标题所要求的那样。UserMootations数组中的另一个true出现了什么问题?我现在看到您以不同的方式穿插了这个问题。我理解第一个是指第一个数组,你把它理解为第一个布尔值。也许你是对的。问题
现在我又读了一遍这个问题,是的,它是不明确的,我指的是第一个数组。userPercentages.reducea,b=>a+b,0。出于好奇过滤器中的u,part做什么?u只是一个变量,没有使用。