Skip to content

Instantly share code, notes, and snippets.

@xyhp915
Created March 14, 2015 16:53
Show Gist options
  • Save xyhp915/4d33cecb991fe0d221d9 to your computer and use it in GitHub Desktop.
Save xyhp915/4d33cecb991fe0d221d9 to your computer and use it in GitHub Desktop.
For Zili homework
//implements it for Zili
(function(data){
// console.log(data)
var AggregationData = {} // 用字面量创建一个空对象
var len = data.length
for( var i = 0, item, barcode; i<len ; i++ ){
item = data[i]
barcode = item['barcode']
//检查聚合数据里面是否统计过该单品
if( AggregationData.hasOwnProperty( barcode ) ){
//统计价格
// AggregationData[ barcode ]['price'] += item['price']
//统计数量
AggregationData[ barcode ]['counts']++
}else{
item['counts'] = 1
AggregationData[ barcode ] = item
}
}
//输出模板
var tpl = [
'***<没钱赚商店>购物清单***',
'X', //内容占位 [1]
'----------------------',
'总计:{totalFee}(元)',
'**********************'
]
var items = [], item, totalFee = 0, t
for( var barcode in AggregationData){
item = AggregationData[barcode]
items.push(
'名称:'+item['name']+',数量:'+item['counts']+item['unit']+',单价:'
+ parseFloat(item['price']).toFixed(2)
+ '(元),小计:'
+ ( t = parseFloat(item['price'])*item['counts'], totalFee+=t, t ).toFixed(2)
+ '(元)'
)
}
//拼接模板
tpl[1] = items.join('\n')
tpl = tpl.join('\n')
//替换总价
tpl = tpl.replace('{totalFee}', totalFee.toFixed(2))
console.log(tpl)
})(inputs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment