Testtest
Testtest

Reputation: 91

How to count specific no of items in Postman response

I am new to postman. I have response and I want to count how many items with equipment_id =53 Here is my code which I tried but I couldn't see any output in console.

Response

[
    {
        "id": 373,
        "user_id": 119118855,
        "location_id": 9999,
        "duration": 0,
        "watts": 0,
        "timestamp": "2019-07-12T00:00:00.000Z",
        "equipment_id": 53,
        "name": "10 Ride",
        "equipment_name": "Bike2"
    },
    {
        "id": 376,
        "user_id": 119118855,
        "location_id": 9999,
        "duration": 0,
        "watts": 0,
        "timestamp": "2019-06-13T00:00:00.000Z",
        "equipment_id": 53,
        "name": "10 min Ride",
        "equipment_name": "Bike2"
    },
    {
        "id": 338,
        "user_id": 119118855,
        "location_id": 9999,
        "duration": 0,
        "watts": 0,
        "timestamp": "2019-07-12T00:00:00.000Z",
        "equipment_id": 17,
        "name": "20 min Ride",
        "equipment_name": "Bike"
    },
]

Postman script I tried:

pm.test("count of records with equipment id 53",function(){
const jsonData = pm.response.json();
var count = jsonData.length;
//console.log(count);

function noofrecords (){
    _each(count.equipment_id).to.include(53);
    count++;
    console.log("Count:" + noofrecords);

May I know how to write script to get count of equipment_id =53 Answer should be 2 here, but I am not getting it.

Upvotes: 1

Views: 4140

Answers (1)

PDHide
PDHide

Reputation: 19939

count = jsonData.filter( a => a["equipment_id"] === 53 ).length

this gives the count, here we are using array.filter to get all objects with equipment_id 53

Upvotes: 2

Related Questions