How to fetch data from the array and set the data in message?

I have multiple data in array as shown below:

[
    {
        "maskedAccountNo": "XXXXXXXX0255",
        "encryptedAccountno": "b+FEfnck2stUQrF717l5cg==",
        "originalAccountNo": "000405010255"
    },
    {
        "maskedAccountNo": "XXXXXXXX0310",
        "encryptedAccountno": "vAYKVrONThkhm+ojJNyxlA==",
        "originalAccountNo": "000405010310"
    },
    {
        "maskedAccountNo": "XXXXXXXX9777",
        "encryptedAccountno": "7s5LtrZOFNuXbWyK8uPuZw==",
        "originalAccountNo": "000405009777"
    },
    {
        "maskedAccountNo": "XXXXXXXX9537",
        "encryptedAccountno": "CA6OfyG4e5qilJNU8fidbg==",
        "originalAccountNo": "000401109537"
    },
    {
        "maskedAccountNo": "XXXXXXXX3460",
        "encryptedAccountno": "LghvCavQnj8qdU14ztVI+A==",
        "originalAccountNo": "000401113460"
    },
    {
        "maskedAccountNo": "XXXXXXXX6861",
        "encryptedAccountno": "gbowyGIyNARJs8fBNYSCew==",
        "originalAccountNo": "000401116861"
    },
    {
        "maskedAccountNo": "XXXXXXXX5288",
        "encryptedAccountno": "LRjDbvHN6/GQMzI+nAUNUg==",
        "originalAccountNo": "001805015288"
    },
    {
        "maskedAccountNo": "XXXXXXXX7715",
        "encryptedAccountno": "cRvrdsfsP93OIWLQ8qATLw==",
        "originalAccountNo": "628801537715"
    },
    {
        "maskedAccountNo": "XXXXXXXX0184",
        "encryptedAccountno": "NFPATcX88EAaUSaaKtdu2Q==",
        "originalAccountNo": "000319000184"
    },
    {
        "maskedAccountNo": "XXXXXXXX3586",
        "encryptedAccountno": "GDPSkzAzIKvWBzU99JG/nQ==",
        "originalAccountNo": "000401193586"
    },
    {
        "maskedAccountNo": "XXXXXXXX1139",
        "encryptedAccountno": "T6KpxvMopUUWM58lN0xTkA==",
        "originalAccountNo": "000405111139"
    },
    {
        "maskedAccountNo": "XXXXXXXX1186",
        "encryptedAccountno": "en8YgoFjMxAfaVDbSMVV2Q==",
        "originalAccountNo": "000405501186"
    },
    {
        "maskedAccountNo": "XXXXXXXX4994",
        "encryptedAccountno": "zGtoF8nzwth17pwiLsrdsQ==",
        "originalAccountNo": "000760004994"
    },
    {
        "maskedAccountNo": "XXXXXXXX5689",
        "encryptedAccountno": "rl/r918gcqR9fxQ5PwWA/A==",
        "originalAccountNo": "000805015689"
    },
    {
        "maskedAccountNo": "XXXXXXXX1320",
        "encryptedAccountno": "PMCqd48NUoCm/qO5Ck2+ng==",
        "originalAccountNo": "000813001320"
    },
    {
        "maskedAccountNo": "XXXXXXXX4982",
        "encryptedAccountno": "YJLMVfIM8l7da7Hb7G3g1w==",
        "originalAccountNo": "001105024982"
    },
    {
        "maskedAccountNo": "XXXXXXXX0623",
        "encryptedAccountno": "E6i+PxeUrv1zZBIqVlYnnQ==",
        "originalAccountNo": "001801080623"
    },
    {
        "maskedAccountNo": "XXXXXXXX2371",
        "encryptedAccountno": "OJ3pSfPa1cdq/L/hZl5Z2A==",
        "originalAccountNo": "001810042371"
    },
    {
        "maskedAccountNo": "XXXXXXXX5268",
        "encryptedAccountno": "WlthXvuui/BQUeKircVQvw==",
        "originalAccountNo": "001825075268"
    },
    {
        "maskedAccountNo": "XXXXXXXX9637",
        "encryptedAccountno": "Y94uyOwp1wLFvB9892Dg8g==",
        "originalAccountNo": "004214109637"
    },
    {
        "maskedAccountNo": "XXXXXXXX9638",
        "encryptedAccountno": "QuxxxLWwNCeIosmc9v/aqA==",
        "originalAccountNo": "004214109638"
    },
    {
        "maskedAccountNo": "XXXXXXXX0278",
        "encryptedAccountno": "pab//kSfNfRvCcnQM+M4hw==",
        "originalAccountNo": "010310000278"
    },
    {
        "maskedAccountNo": "XXXXXXXX4151",
        "encryptedAccountno": "kHJ28ZD8bowEdxN6XjpoGQ==",
        "originalAccountNo": "032501004151"
    },
    {
        "maskedAccountNo": "XXXXXXXX8102",
        "encryptedAccountno": "GEq9da051Jy55yXqlBqwGw==",
        "originalAccountNo": "044205008102"
    },
    {
        "maskedAccountNo": "XXXXXXXX0275",
        "encryptedAccountno": "2F3ZngaXpl3bbHV5z4L6/A==",
        "originalAccountNo": "105701000275"
    },
    {
        "maskedAccountNo": "XXXXXXXX5140",
        "encryptedAccountno": "lWqBrzaA4KclEPv/pfKMEg==",
        "originalAccountNo": "630010075140"
    }
]

I used the Extract Variable to fetch the data and after i used JS policy for set the data but not able to set in JS. I just want to fetched the above data and set the data as shown below. Now i want to fetch one by one data and set in the message as shown below:

[
        {
            "accNumber": "XXXXXXXX0255",
            "type": "Current",
            "encAccNumber": "tRdqDpBF7Q9L/DLn1LFonK7m429L2BHHZAiraGGTeh4=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX0310",
            "type": "Current",
            "encAccNumber": "zdOF51PXO/Cc9CHWpfchPLRbs94+CpHkVrAUbTQkzkI=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX9777",
            "type": "Current",
            "encAccNumber": "wsMtUPiSXRux2i1JEuG6A7WUTJ/TsVZGBCOYJ19equg=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX9537",
            "type": "Saving",
            "encAccNumber": "gbmZhBAsrvKj3fIPwsrPCb/5SKWokhYP56vHdgi0CGY=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX3460",
            "type": "Saving",
            "encAccNumber": "wmujDWyWZNAPX977W0ixn3c8OLnpgr4+7/yrKDDWc1s=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX6861",
            "type": "Saving",
            "encAccNumber": "gpCfbDfUsM3Jfy1e7AHVVSjntAHS7KPRIWvt7GE1E2g=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX5288",
            "type": "Current",
            "encAccNumber": "ZT805QA2IgI7NoFPg/Vc9C+qRZmISWK/8cIgPCd6sB4=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX7715",
            "type": "Saving",
            "encAccNumber": "PXCJsRqMNXIvO0dGWrAsj0LHNFdgm3ymIdDVwf7dvnI=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX3586",
            "type": "Saving",
            "encAccNumber": "LSoIQeT/iBEz1uYIkvobTxzGpq8QxLrq1oa2FL5PLlA=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX1139",
            "type": "Current",
            "encAccNumber": "ieDhInP5BSUi+nYb19mUX0AyFr7PrJpI4G2H5qOEOcg=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX1186",
            "type": "Current",
            "encAccNumber": "qMcaEyrNfopUaQfGMNw8lwLEOFF+mUpjw1YRKB4FJLE=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX5689",
            "type": "Current",
            "encAccNumber": "s1iPLxE13rw5lRMExZqFgW89nmWMT/OxCTD99+NnA/A=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX4982",
            "type": "Current",
            "encAccNumber": "FPVTu77BvywdniH1xpkLq2wqDoLwFQT5VA1ch5yZOlg=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX0623",
            "type": "Saving",
            "encAccNumber": "DaG1VXDasEj+fA+cQCFrZjBATWB4i1bMSksOG/86oTU=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX4151",
            "type": "Saving",
            "encAccNumber": "I/Z8QCX9rGkdSmpRr9unHdXf42QVaQ4CvM7qW4KmXos=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX8102",
            "type": "Current",
            "encAccNumber": "Lu20/mxn0sRWlGxLHJIBEIt5aW3Wrwj6WnVkNIuNVOc=",
            "status": "Active"
        },
        {
            "accNumber": "XXXXXXXX0275",
            "type": "Saving",
            "encAccNumber": "KDJeDtSzOmarT5mSpyqOhFSCNavU/xRKgKgsFSi4AUk=",
            "status": "Active"
        }
    ]

Can anyone help me to write the JS code for the above data?

0 2 2,069
2 REPLIES 2

Thanks for all the details. Even so, I'm not clear. You start with an array of 26 items, and the desired end is an array of 17 items. You haven't provided a rule or guide for which items you want to remove.

Also you want to change each remaining item. Somehow the encryptedAccountno in the original mapps to the encAccNumber in the desired end object, but you haven't explained how to do that either. (They do not appear to be the same string).

I don't think you've explained the problem clearly. I think you understand the problem, but your description hasn't explained it to us.

I might be able to help you solve your own problem, though. It seems to be a question of the correct JavaScript to use. This is not an Apigee issue.

When we transform an array of one thing into an array of equal length of another thing, we call that process "a mapping process" or "a map transform". In Javascript there's a nice map() function on the array object that supports this. The map function accepts... a function. The inner function accepts a thing (anything) and returns something else. These things can be of any type. A simple example is a function that accepts a number and returns a number. In JavaScript,

var a = [1,2,3];
function x2(value) { return 2 * value; }
a.map(x2); // [2,4,6]

The function you pass to map() can accept an object. Suppose you have an object like this:

    {
        "maskedAccountNo": "XXXXXXXX5140",
        "encryptedAccountno": "lWqBrzaA4KclEPv/pfKMEg==",
        "originalAccountNo": "630010075140"
    }

And you want to transform that into a differently shaped object, like this:

    {
        "accNumber": "XXXXXXXX5140",
        "status": "Active"
    }

To do that, you can use a function.

function xform(obj) {
  return {
    accNumber: obj.maskedAccountNo,
    status: "Active"
  };
}

If you then have an array of objects of the original form, then you can just map that array to an array of objects of the second form, using the map() function.

var newArray = a.map(xform); 

So that's mapping in JavaScript.

Getting back to your request, each of the objects of the desired end state have fields "encAccNumber" and "type". But you didn't describe how you get the values "encAccNumber" and "type" from the original objects. Therefore I cannot describe how to solve that. I'll leave that for you to solve. You will have to modify your "xform" function to make it return objects in the shape that you want. You also didn't describe how to reduce the elements in the array from 26 to 17. You might want to use the array .filter() function for that.

Last thing: I suggest you try this out in a JS development environment, outside of Apigee, before embedding it into a policy. Developing and testing is easier using node or some other lighter-weight JS-oriented environment.

Good luck.

Any response?