Javascript compare items in one array

后端 未结 5 1594
被撕碎了的回忆
被撕碎了的回忆 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:37

    The brute force method would be something like this:

    var users = []; // here is where you get the users from the device
    var goodUsers = []; // here you get your good users list
    var badUsers = []; // initialize an empty array for bad users
    
    for (var i=0; i< users.length; i++) {
        var isAGoodUser = false;
        for(var y=0; y< goodUsers.length; y++) {
            if(users[i] == goodUsers[y]) {
                printf('Username: ' + users[i] + ' is good\n');
                isAGoodUser = true;
                break;
            }
        }
        if(!isAGoodUser){
            printf('Username: ' + users[i] + ' is NOT good\n');
            badUsers.push(users[i]);
        }
    }
    

提交回复
热议问题