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

时间:2018-05-08 20:00:11

标签: javascript arrays paypal-nvp

以下是Paypal NVP API的回复。我不熟悉使用像这样的键/值的单个对象的响应。通常情况下,我希望搜索能够以包含对象的数组形式返回。

有没有办法让我把它变成一个物体数组?

{
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
]

2 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

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 :(得分:1)

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

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

  1. 获取对象的键
  2. 将密钥过滤到您想要的编号
  3. 将密钥映射到关于每个
  4. 的小型信息对象
  5. 将键列表重新组织为对象列表,其中对象中的原始索引数据位于正确的索引处。
  6. 这可以在一行中完成,最终结果是一个recentOrders变量设置为3个对象的数组,其中命名键是小写的(我认为这就是你所追求的)

    Could not import the PyAudio C module '_portaudio'