troubleshooting Question

Postman Test (Javascript) issue with looping an array

Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America asked on
JavaScript* postman
4 Comments1 Solution68 ViewsLast Modified:
Let's say that I have the following Postman test:
    
    var U = data.vCustomer.userDefFlds;
    tests["UDF1  : Matched"] = U[0].udfValue === "CUST_UDF1";
    tests["UDF2  : Matched"] = U[11].udfValue === "CUST_UDF2";
    tests["UDF3  : Matched"] = U[22].udfValue === "CUST_UDF3";
    tests["UDF4  : Matched"] = U[33].udfValue === "CUST_UDF4";
    tests["UDF5  : Matched"] = U[44].udfValue === "CUST_UDF5";
    tests["UDF6  : Matched"] = U[46].udfValue === "CUST_UDF6";
    tests["UDF7  : Matched"] = U[47].udfValue === "CUST_UDF7";
    tests["UDF8  : Matched"] = U[48].udfValue === "CUST_UDF8";
    tests["UDF9  : Matched"] = U[49].udfValue === "CUST_UDF9";
    tests["UDF10 : Matched"] = U[1].udfValue === "CUST_UDF10";
    tests["UDF11 : Matched"] = U[2].udfValue === "CUST_UDF11";
    tests["UDF12 : Matched"] = U[3].udfValue === "CUST_UDF12";
    tests["UDF13 : Matched"] = U[4].udfValue === "CUST_UDF13";
    tests["UDF14 : Matched"] = U[5].udfValue === "CUST_UDF14";
    tests["UDF15 : Matched"] = U[6].udfValue === "CUST_UDF15";
    tests["UDF16 : Matched"] = U[7].udfValue === "CUST_UDF16";
    tests["UDF17 : Matched"] = U[8].udfValue === "CUST_UDF17";
    tests["UDF18 : Matched"] = U[9].udfValue === "CUST_UDF18";
    tests["UDF19 : Matched"] = U[10].udfValue === "CUST_UDF19";
    tests["UDF20 : Matched"] = U[12].udfValue === "CUST_UDF20";
    tests["UDF21 : Matched"] = U[13].udfValue === "CUST_UDF21";
    tests["UDF22 : Matched"] = U[14].udfValue === "CUST_UDF22";
    tests["UDF23 : Matched"] = U[15].udfValue === "CUST_UDF23";
    tests["UDF24 : Matched"] = U[16].udfValue === "CUST_UDF24";
    tests["UDF25 : Matched"] = U[17].udfValue === "CUST_UDF25";
    tests["UDF26 : Matched"] = U[18].udfValue === "CUST_UDF26";
    tests["UDF27 : Matched"] = U[19].udfValue === "CUST_UDF27";
    tests["UDF28 : Matched"] = U[20].udfValue === "CUST_UDF28";
    tests["UDF29 : Matched"] = U[21].udfValue === "CUST_UDF29";
    tests["UDF30 : Matched"] = U[23].udfValue === "CUST_UDF30";
    tests["UDF31 : Matched"] = U[24].udfValue === "CUST_UDF31";
    tests["UDF32 : Matched"] = U[25].udfValue === "CUST_UDF32";
    tests["UDF33 : Matched"] = U[26].udfValue === "CUST_UDF33";
    tests["UDF34 : Matched"] = U[27].udfValue === "CUST_UDF34";
    tests["UDF35 : Matched"] = U[28].udfValue === "CUST_UDF35";
    tests["UDF36 : Matched"] = U[29].udfValue === "CUST_UDF36";
    tests["UDF37 : Matched"] = U[30].udfValue === "CUST_UDF37";
    tests["UDF38 : Matched"] = U[31].udfValue === "CUST_UDF38";
    tests["UDF39 : Matched"] = U[32].udfValue === "CUST_UDF39";
    tests["UDF40 : Matched"] = U[34].udfValue === "CUST_UDF40";
    tests["UDF41 : Matched"] = U[35].udfValue === "CUST_UDF41";
    tests["UDF42 : Matched"] = U[36].udfValue === "CUST_UDF42";
    tests["UDF43 : Matched"] = U[37].udfValue === "CUST_UDF43";
    tests["UDF44 : Matched"] = U[38].udfValue === "CUST_UDF44";
    tests["UDF45 : Matched"] = U[39].udfValue === "CUST_UDF45";
    tests["UDF46 : Matched"] = U[40].udfValue === "CUST_UDF46";
    tests["UDF47 : Matched"] = U[41].udfValue === "CUST_UDF47";
    tests["UDF48 : Matched"] = U[42].udfValue === "CUST_UDF48";
    tests["UDF49 : Matched"] = U[43].udfValue === "CUST_UDF49";
    tests["UDF50 : Matched"] = U[45].udfValue === "CUST_UDF50";
The tests are written in javascript.
I want to replicate this using only a for loop but I don't think a simple loop will work because CUST_UDF10 = U[1] & CUST_UDF2 = U[11];
If I sort the array U, that gives me issues with CUST_UDF1, CUST_UDF10, and CUST_UDF32.

What am I doing wrong here?

    U.sort();
    for(i=0;i<50;i++) {
        x = i+1;
        tests["UDF"+x+"  : Matched"] = U[i].udfValue === "CUST_UDF"+x;
    }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros