0

Below is the response from Paypal's NVP API. I am not familiar with working with a response that is a single object of keys/values like this. Typically I would expect for a search to be return in a form of an array with objects within.

Is there a way for me to turn this into an Array of Objects?

{
L_TIMESTAMP0: "2018-05-08T19:23:17Z",
L_TIMESTAMP1: "2018-05-08T18:50:01Z",
L_TIMESTAMP2: "2018-05-08T18:45:30Z",
L_TIMEZONE0: "GMT",
L_TIMEZONE1: "GMT",
L_TIMEZONE2: "GMT",
L_TYPE0: "Payment",
L_TYPE1: "Payment",
L_TYPE2: "Payment",
L_NAME0: "Person One",
L_NAME1: "Person Two",
L_NAME2: "Person Three",
L_TRANSACTIONID0: "*********",
L_TRANSACTIONID1: "*********",
L_TRANSACTIONID2: "*********",
L_STATUS0: "Completed",
L_STATUS1: "Completed",
L_STATUS2: "Completed",
L_AMT0: "10.00",
L_AMT1: "100.00",
L_AMT2: "1000.00",
L_CURRENCYCODE0: "USD",
L_CURRENCYCODE1: "USD",
L_CURRENCYCODE2: "USD",
L_FEEAMT0: "-0.29",
L_FEEAMT1: "-2.93",
L_FEEAMT2: "-29.30",
L_NETAMT0: "9.71",
L_NETAMT1: "97.70",
L_NETAMT2: "970.70",
TIMESTAMP: "2018-05-08T19:47:10Z", // not for array
CORRELATIONID: "*******", // not for array
ACK: "Success", // not for array
VERSION: "204", // not for array
BUILD: "39949200" // not for array
}

I would like parse this into an array of objects:

const recentOrders = [{
    timestamp: L_TIMESTAMP0,
    timezone: L_TIMEZONE0,
    type: L_TYPE,
    .....
},
{ timestamp: L_TIMESTAMP1, .... },
{ timestamp: L_TIMESTAMP2, .... },
// .... and so forth
]
Dadsquatch
  • 566
  • 5
  • 16

2 Answers2

1

Something like this should work:

var data = {
L_TIMESTAMP0: "2018-05-08T19:23:17Z",
L_TIMESTAMP1: "2018-05-08T18:50:01Z",
L_TIMESTAMP2: "2018-05-08T18:45:30Z",
L_TIMEZONE0: "GMT",
L_TIMEZONE1: "GMT",
L_TIMEZONE2: "GMT",
L_TYPE0: "Payment",
L_TYPE1: "Payment",
L_TYPE2: "Payment",
L_NAME0: "Person One",
L_NAME1: "Person Two",
L_NAME2: "Person Three",
L_TRANSACTIONID0: "*********",
L_TRANSACTIONID1: "*********",
L_TRANSACTIONID2: "*********",
L_STATUS0: "Completed",
L_STATUS1: "Completed",
L_STATUS2: "Completed",
L_AMT0: "10.00",
L_AMT1: "100.00",
L_AMT2: "1000.00",
L_CURRENCYCODE0: "USD",
L_CURRENCYCODE1: "USD",
L_CURRENCYCODE2: "USD",
L_FEEAMT0: "-0.29",
L_FEEAMT1: "-2.93",
L_FEEAMT2: "-29.30",
L_NETAMT0: "9.71",
L_NETAMT1: "97.70",
L_NETAMT2: "970.70",
TIMESTAMP: "2018-05-08T19:47:10Z", // not for array
CORRELATIONID: "*******", // not for array
ACK: "Success", // not for array
VERSION: "204", // not for array
BUILD: "39949200" // not for array
};

const recentOrders = [];

var keys = Object.keys(data).filter(r => r.startsWith('L_'));

keys.forEach(k => {
    var index = parseInt(k.replace(/\D/g,''));
    var newKey = k.substring(2).replace(/[0-9]/g, '').toLowerCase();
    if (recentOrders[index] === undefined) {
        recentOrders[index] = {};
    }
    recentOrders[index][newKey] = data[k];
});

console.log(recentOrders);
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    I'd hug you if I could. That's exactly what I needed. will be disecting this to try and learn each part of it. – Dadsquatch May 09 '18 at 12:04
1

I'm not entirely sure what the output you want is, but fundamentally you want to map and transform the data from the very flat structure you are given to more of a list of related objects.

You can do this using some of the basic native methods of arrays to filter map and reorganize your data. The following code basically does the following:

  1. Get the keys of the object
  2. Filter the keys to the numbered ones you want
  3. Map the keys to a small informational object about each
  4. Reorganize the list of keys into a list of objects with the original indexed data in the objects at the correct index.

This can be done in one line with the end result being a a recentOrders variable set to an array of 3 objects with the named keys lowercased (which I think is what you are after)

const recentOrders = Object.keys(data)
    .filter(function (key) { 
        return key.startsWith('L_') 
    })
    .map(function (key) { 
        const parts = key.match(/^L_([A-Z]+)(\d+)$/); 
        return { 
            key: parts[1].toLowerCase(),
            value: data[key], 
            index: parseInt(parts[2]) 
        };  
    })
    .reduce(function (recentOrders, item) { 
        if (recentOrders[item.index] == null) { 
            recentOrders[item.index] = {} 
        } 
        recentOrders[item.index][item.key] = item.value; 
        return recentOrders ;
    }, []);

console.log(recentOrders);
musicfuel
  • 1,532
  • 10
  • 7