2016-04-27 53 views
0

從其中拉取數據並重新格式化它。每個組按日期在咖啡因

Promise = require "bluebird" 
request = Promise.promisify require "request" 
moment = require "moment" 
cdn = require('config').server.cloudFrontDomain 
toTitleCase = require "titlecase" 

exports.getStocks = (path) -> 
    return new Promise (resolve, reject) -> 
    request path 
     .then (body) -> 
     germanStock = [] 
     germanStocks = JSON.parse body.body 
     germanStocks.forEach (stock) -> 
      obj = {} 
      this.parsePart = (remaining) -> 
      value = remaining.value 
      dashIndex = value.lastIndexOf '-' 
      if dashIndex != -1 
       remaining.value = value.substring 0, dashIndex - 1 
       return value.substring(dashIndex + 1).trim() 
      else 
       return '' 

      remaining = 
      value: stock.name 
      size = parsePart remaining 
      colour = parsePart remaining 
      name = remaining.value 
      sku = stock.sku 
      styleId = sku.split(/-/)[0] 
      colorcode = /^(.*)-(.*)([0-9])$/.exec(sku)?[2] 
      bgStyle = "url(//#{cdn}/assets/product_shots/thumbs/#{styleId}-#{colorcode}.jpg)" 

      obj.id = sku 
      obj.name = name 
      obj.colorUrl = bgStyle 
      obj.colour = toTitleCase(colour.toLowerCase()) 
      obj.size = size 
      obj.stock = stock.stock 
      obj.inProduction = '' 
      obj.office = 'DE' 

      stock.preorders.forEach (i, idx) -> 
      date = moment(i.date).format('DD-MM-YYYY') 
      if idx != stock.preorders.length - 1 
       obj.inProduction = obj.inProduction.concat i.amount + ' due on ' + date + ', ' 
      else 
       obj.inProduction = obj.inProduction.concat i.amount + ' due on ' + date 
      germanStock.push obj 
     resolve germanStock 

     .catch (err) -> 
     reject err 

在我的數據是這樣的:

{ 
    "id":1, 
    "stamp":"2014-09-25T12:55:30Z", 
    "name":" MENS T-SHIRT - BRIGHT BLUE - XS", 
    "sku":"SS01-BB0", 
    "stock":81, 
    "active":true, 
    "preorders":[ 
     { 
     "id":92549, 
     "amount":160, 
     "date":"2016-06-19T22:00:00Z" 
     }, 
     { 
     "id":92549, 
     "amount":200, 
     "date":"2016-06-19T22:00:00Z" 
     }, 
     { 
     "id":92549, 
     "amount":1000, 
     "date":"2016-06-21T22:00:00Z" 
     } 
    ], 
    "discountMatrix":0.0, 
    "stockNormalized":81, 
    "preOrdersSum":1360 
}, 
{ 
    "id":2, 
    "stamp":"2014-09-25T12:55:30Z", 
    "name":" MENS T-SHIRT - BRIGHT BLUE - S", 
    "sku":"SS01-BB1", 
    "stock":339, 
    "active":true, 
    "preorders":[ 
     { 
     "id":92551, 
     "amount":240, 
     "date":"2016-06-19T22:00:00Z" 
     }, 
     { 
     "id":92438, 
     "amount":160, 
     "date":"22016-06-19T22:00:00Z" 
     } 
    ], 
    "discountMatrix":0.0, 
    "stockNormalized":339, 
    "preOrdersSum":400 
}, 

什麼是正確的方法來組各preorders量是在同一天,這樣不但得不到:

160 due on 19-06-2016, 200 due on 19-06-2016, 1000 due on 21-06-2016

我收到360 due on 19-06-2016, 1000 due on 21-06-2016

非常感謝任何意見。

回答

1

您可以將日期作爲關鍵字和日期總量作爲值使用。

對於每個預購,請將其數量添加到該對象的日期索引中。在迭代結束打印對象的內容:

moment = require "moment" 

data = [ 
    { 
    id:1 
    stamp: "2014-09-25T12:55:30Z" 
    name: " MENS T-SHIRT - BRIGHT BLUE - XS" 
    sku: "SS01-BB0" 
    stock:81 
    active:true 
    preorders:[ 
     { 
     id:92549 
     amount:160 
     date: "2016-06-19T22:00:00Z" 
     } 
     { 
     id:92549 
     amount:200 
     date: "2016-06-19T22:00:00Z" 
     } 
     { 
     id:92549 
     amount:1000 
     date: "2016-06-21T22:00:00Z" 
     } 
    ] 
    discountMatrix:0.0 
    stockNormalized:81 
    preOrdersSum:1360 
    } 
] 

obj = {} 
obj.inProduction = "" 
amountByDate = {} 

# for each document in your data 
for doc in data 
    # for each preorder in your document 
    for preorder in doc.preorders 
    # add it's amount in the index equals to it's date 
    if amountByDate[preorder.date] 
     amountByDate[preorder.date] += preorder.amount 
    else 
     # or create the index with the value if it doesn't exist 
     amountByDate[preorder.date] = preorder.amount 

for date, amount of amountByDate 
    if obj.inProduction != "" 
    obj.inProduction = obj.inProduction.concat ", #{amount} due on #{moment(date).format('DD-MM-YYYY')}" 
    else 
    obj.inProduction = obj.inProduction.concat "#{amount} due on #{moment(date).format('DD-MM-YYYY')}" 

console.log obj.inProduction 

結果:

360由於上20-06-2016,1000由於上22-06-2016