在节点js中连接json结果

时间:2017-05-14 18:39:42

标签: json node.js

我正在编写一个执行以下操作的节点程序。

  • 扫描DynamoDB并获得结果。
  • 根据结果做以下操作。
    • 如果返回值的大小为1. console.log该值。
    • 如果结果超过1,则结果并将其打印到控制台。 目前我能够得到结果并在控制台中打印出来。如果它大于一,我很困惑我怎么能这样做。

以下是我的代码。

function getMyDueResponses(response, session) {
var responseText='';
  console.log('Here is your result' + session.attributes.userData.Count);
  console.log(session.attributes.userData.length);
  if (session.attributes.userData.Count < 2) {
    var res = session.attributes.userData;
    var userDueDate = JSON.stringify(res.Items[0].dueDate);
    var userDueAmount = JSON.stringify(res.Items[0].dueAmount);
    var userTotalBalance = JSON.stringify(res.Items[0].totalBalance);
    responseText = `Your next Chubb bill is due on ${userDueDate}. The payment due is ${userDueAmount}$. The full account balance is ${userTotalBalance}$.`;
    console.log(responseText);
}
  else {
    //Here I'm stuck on how to proceed.
    }
}
例如,

。 DB返回了以下数据。

{
    "Items": [
        {
            "accountId": "12345",
            "pin": "1234",
            "userId": "user1",
            "dueDate": "5/20/2017",
            "_id": "2",
            "dueAmount": "4000",
            "totalBalance": "10000"
        }
    ],
    "Count": 1,
    "ScannedCount": 4
}

响应应该是。我能够得到这个结果。

Your bill is due on 5/20/2017. The payment due is 4000$. The full account balance is 10000$.

db返回了以下数据。

{
    "Items": [
        {
            "accountId": "12345",
            "pin": "1234",
            "userId": "user1",
            "dueDate": "5/20/2017",
            "_id": "2",
            "dueAmount": "4000",
            "totalBalance": "10000"
        },
        {
            "accountId": "12345",
            "pin": "1234",
            "userId": "user1",
            "dueDate": "5/23/2017",
            "_id": "2",
            "dueAmount": "1000",
            "totalBalance": "10000"
        },
        {
            "accountId": "12345",
            "pin": "1234",
            "userId": "user1",
            "dueDate": "5/24/2017",
            "_id": "2",
            "dueAmount": "300",
            "totalBalance": "10000"
        }
    ],
    "Count": 3,
    "ScannedCount": 4
}

以下数据必须打印在console.log()

Your bill 1 is due on 5/20/2017. The payment due is 4000$. The full account balance is 10000$. Your bill 2 is due on 5/23/2017. The payment due is 1000$. The full account balance is 10000$. Your bill 3 is due on 5/24/2017. The payment due is 300$. The full account balance is 10000$.

请让我知道我该怎么做。

1 个答案:

答案 0 :(得分:2)

为什么不为项目数量运行循环?

编辑:您可以在循环时将每个响应添加到数组,然后使用Reduce()将数组作为单个字符串打印;

    var arr = [];
    var res = session.attributes.userData;


    for (var x = 0; x < session.attributes.userData.Count; x++) {

        var userDueDate = JSON.stringify(res.Items[x].dueDate);
        var userDueAmount = JSON.stringify(res.Items[x].dueAmount);
        var userTotalBalance = JSON.stringify(res.Items[x].totalBalance);
        responseText = `Your next Chubb bill is due on ${userDueDate}. The payment due 
        is ${userDueAmount}$. The full account balance is ${userTotalBalance}$.`;
        arr.push(responseText);
    }

console.log(arr.reduce(function(acc, cur) { return acc + cur; }