2017-05-14 86 views
0

我正在寫一個節點程序,執行下面的操作。concat json結果在節點js

  • 掃描DynamoDB並獲取結果。
  • 根據結果做到以下幾點。
    • 如果返回值的大小爲1. console.log的值。
    • 如果結果超過1個concat結果並打印到控制檯。 目前我能夠得到結果並在控制檯中打印。如果它大於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. 
    } 
} 

例如。 數據庫返回了下面的數據。

{ 
    "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$. 

請讓我知道我該怎麼做。

回答

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; } 
+0

感謝您的快速建議,這不符合我的情況。我知道循環會完成這項工作。但是我想在循環完成後進行控制檯打印......我聽說這個東西可以使用承諾實現,但我不確定這個東西是如何工作的。你能幫我解決這個問題嗎? – user3872094

+0

我使用Reduce編輯了我的答案,也許這是你需要的嗎? –