【问题标题】:i don't know how to us js filter index我不知道如何使用 js 过滤器索引
【发布时间】:2021-06-07 15:39:03
【问题描述】:
const arrayRcp = ["ROCK", "Scissors", "ROCK", "Scissors"]
const user = ["par1", "par2", "par3", "par4"]
const winner = arrayRcp.filter(item => item == 'ROCK' )
const winnerUser = arrayRcp.filter(function(item,index) {...???} )

我也想计算获胜者对获胜者用户的 ROCK 指数。 然后我不知道...???如何使用过滤器索引该功能请帮助我...

由于ROCK索引0和2出现在arrayRcp中,我想知道如何通过过滤器获取par1和par3,就像用户中ROCK的索引一样。

【问题讨论】:

标签: javascript filter


【解决方案1】:

试试这个:

const arrayRcp = ["ROCK", "Scissors",  "ROCK", "Scissors"]
const user = ["par1", "par2", "par3", "par4"]
const winner = arrayRcp
  .map((item, index) => item === 'ROCK' && index)
  .filter(Number.isFinite)

const winnerUser = user.filter(function(_, index) {
  return this.indexOf(index) >= 0
} , winner)

console.log(winnerUser)

【讨论】:

    猜你喜欢
    • 2011-05-04
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2013-04-25
    相关资源
    最近更新 更多