Javascript 将响应对象解析为对象数组

Javascript parse Response Object into Array of Objects

以下是 Paypal 的 NVP API 的回复。我不熟悉像这样处理作为 keys/values 的单个对象的响应。通常我希望搜索 return 以数组的形式出现,其中包含对象。

我有办法把它变成对象数组吗?

{
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 = [{
    timestamp: L_TIMESTAMP0,
    timezone: L_TIMEZONE0,
    type: L_TYPE,
    .....
},
{ timestamp: L_TIMESTAMP1, .... },
{ timestamp: L_TIMESTAMP2, .... },
// .... and so forth
]

像这样的东西应该可以工作:

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);

我不完全确定您想要的输出是什么,但从根本上说,您希望将数据从给定的非常扁平的结构映射和转换为更多相关对象列表。

您可以使用一些基本的数组本机方法来过滤映射和重新组织数据。以下代码基本上执行以下操作:

  1. 获取对象的键
  2. 将键过滤为您想要的编号
  3. 将键映射到关于每个键的小信息对象
  4. 将键列表重组为对象列表,对象中的原始索引数据位于正确的索引处。

这可以在一行中完成,最终结果是一个 recentOrders 变量设置为一个包含 3 个对象的数组,命名键小写(我认为这就是你想要的)

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);