vue 循环数组问题for

以前的代码是必需要五个selectGame.id参数,我就用的最笨的办法,现在的要求是1-10个,
mylotteryList1和selectGame中都有id,相互对应
我之前的思路是只要mylotteryList1中包含有selectGame的id,就把mylotteryList1中的那组数据push进collectionList中
请问如何更改?

for(let j=0;j<this.mylotteryList1.length;j++){
    let gid = this.mylotteryList1[j].Id
        if(gid==this.selectGame[0].Id||gid==this.selectGame[1].Id||gid==this.selectGame[2].Id||gid==this.selectGame[3].Id||gid==this.selectGame[4].Id){
            this.collectionList.push(this.mylotteryList1[j])
        }
 }
阅读 1.8k
2 个回答

前端都ES2018了,那么多美妙直观的语法糖不用,看到这种丑到天际的for j for i真是无语啊
es6一行就行了

this.collectionList.push(...this.mylotteryList1.filter(lottery => this.selectGame.some(game => lottery.Id === game.Id)));
for(let j=0;j<this.mylotteryList1.length;j++){
    let gid = this.mylotteryList1[j].Id
    for(let i = 0;i< this.selectGame.length;i++){
        if(gid === this.selectGame[i].Id){
            this.collectionList.push(gid)
            return
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题