Get Multiple Values With In An Array

‘’'Hi I’m new, hope someone could help, so below is the response that I’m getting on the end point I’m working on. The end point filters the accounts that the user will search by userid or gameid.
The end point is working well at the moment. What I’m trying to achieve is , to check all user id with in the array has the same value (To make sure that the API is filtering query properly). Included is my test script, the script works well and is able to check all the value in user_id, (I’m using variables by the way.). My concern is the end points get updated almost every day, Accounts are being deleted and added everyday. Is there a way to make my script shorter and able to cope up with the updates. thanks

*Details *:

{

"error": false,

"message": "Successfully loaded ",

"data": {

    "accounts": [

        {

            "id": 7,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 2,

            "game_account_limit": 7,

            "wallet_address": "20dadawd",

            "game_accounts": []

        },

        {

            "id": 8,

            "user_id": 8,

            "game_id": 3,

            "guild_id": 2,

            "game_account_limit": 13,

            "wallet_address": "card",

            "game_accounts": []

        },

        {

            "id": 9,

            "user_id": 8,

            "game_id": 3,

            "guild_id": 2,

            "game_account_limit": 13,

            "wallet_address": "feed",

            "game_accounts": []

        },

        {

            "id": 11,

            "user_id": 8,

            "game_id": 3,

            "guild_id": 2,

            "game_account_limit": 13,

            "wallet_address": "feed",

            "game_accounts": []

        },

        {

            "id": 12,

            "user_id": 8,

            "game_id": 3,

            "guild_id": 2,

            "game_account_limit": 13,

            "wallet_address": "interface",

            "game_accounts": []

        },

        {

            "id": 14,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "system",

            "game_accounts": []

        },

        {

            "id": 17,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "alarm",

            "game_accounts": []

        },

        {

            "id": 18,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "sensor",

            "game_accounts": []

        },

        {

            "id": 19,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "interface",

            "game_accounts": []

        },

        {

            "id": 20,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "alarm",

            "game_accounts": []

        }

    ],

    "current_page": 1,

    "total_pages": 2,

    "total_results": 14

}

}

I’ve already tried:

‘’'pm.test('Test: Filter game account via usser id ', function() {

pm.response.to.have.status(200);

});

var jsonData = pm.response.json()

pm.test(“ONLY a declared user id should be returned”, () => {

const responseJson = pm.response.json();

pm.expect(responseJson.data.accounts[0].user_id).to.eql(pm.environment.get(“userid”));

pm.expect(responseJson.data.accounts[1].user_id).to.eql(pm.environment.get(“userid”));

pm.expect(responseJson.data.accounts[2].user_id).to.eql(pm.environment.get(“userid”));

pm.expect(responseJson.data.accounts[3].user_id).to.eql(pm.environment.get(“userid”));

pm.expect(responseJson.data.accounts[4].user_id).to.eql(pm.environment.get(“userid”));

pm.expect(responseJson.data.accounts[5].user_id).to.eql(pm.environment.get(“userid”));

});

1 Like

I am looking for an answer to this too - have you managed to find out how this can be done?

@gabijap

The following should work.

Step 1. Parse the response.
Step 2. Create an array with the info you want to test.
Step 3. Wrap the test into a ForEach loop.

const jsonData = 

{

"error": false,

"message": "Successfully loaded ",

"data": {

    "accounts": [

        {

            "id": 7,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 2,

            "game_account_limit": 7,

            "wallet_address": "20dadawd",

            "game_accounts": []

        },

        {

            "id": 8,

            "user_id": 8,

            "game_id": 3,

            "guild_id": 2,

            "game_account_limit": 13,

            "wallet_address": "card",

            "game_accounts": []

        },

        {

            "id": 9,

            "user_id": 8,

            "game_id": 3,

            "guild_id": 2,

            "game_account_limit": 13,

            "wallet_address": "feed",

            "game_accounts": []

        },

        {

            "id": 11,

            "user_id": 8,

            "game_id": 3,

            "guild_id": 2,

            "game_account_limit": 13,

            "wallet_address": "feed",

            "game_accounts": []

        },

        {

            "id": 12,

            "user_id": 8,

            "game_id": 3,

            "guild_id": 2,

            "game_account_limit": 13,

            "wallet_address": "interface",

            "game_accounts": []

        },

        {

            "id": 14,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "system",

            "game_accounts": []

        },

        {

            "id": 17,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "alarm",

            "game_accounts": []

        },

        {

            "id": 18,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "sensor",

            "game_accounts": []

        },

        {

            "id": 19,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "interface",

            "game_accounts": []

        },

        {

            "id": 20,

            "user_id": 8,

            "game_id": 1,

            "guild_id": 1,

            "game_account_limit": 13,

            "wallet_address": "alarm",

            "game_accounts": []

        }

    ],

    "current_page": 1,

    "total_pages": 2,

    "total_results": 14

}
}

//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));    
        } else 
        //if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
        if (i == key && obj[i] == val || i == key && val == '') { //
            objects.push(obj);
        } else if (obj[i] == val && key == ''){
            //only add if the object is not already in the array
            if (objects.lastIndexOf(obj) == -1){
                objects.push(obj);
            }
        }
    }
    return objects;
}

// const jsonData = pm.response.json()
console.log(jsonData); // This has already been parsed into a JavaScript object

// create new array with just the id, and user_id
var mappedResults = (getObjects(jsonData,'user_id','')).map(({ id, user_id }) => ({ id, user_id }));

// wrap the test in a forEach loop against the array
mappedResults.forEach(element => 
    pm.test('user_id = 8', () => {
        pm.expect(element.user_id).to.eql(8);
    })
);

image

Changing one of the user_id’s to 9 to show a failing test and to confirm that it is indeed looping through the array.

image