Javascript compare items in one array

后端 未结 5 1586
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 00:10

Pardon my complete lack of javascript knowledge in advance, but I can\'t seem to find a good example of how to compare two arrays and create another array based the results.

5条回答
  •  时光说笑
    2021-01-27 00:18

    Use indexOf to find if the user exists in your array.

    var goodUsers = ["someuser1", "someuser2", "someuser3"];
    var storedUsers = ["someuser1", "user", "user3"];
    for(var goodUser in goodUsers){
        if(storedUsers.indexOf(goodUsers[goodUser])>-1){
           console.log('User:' + goodUsers[goodUser] + ' is good.')
           }
    }
        ​
    

提交回复
热议问题