Javascript 从对象返回唯一键列表的正确方法
我有这个密码Javascript 从对象返回唯一键列表的正确方法,javascript,coffeescript,lodash,Javascript,Coffeescript,Lodash,我有这个密码 discounts = { 'N18-AB0': 10, 'N18-AB2': 10, 'N18-BL2': 10, 'N22-WHBL0': 10, 'N22-WHBL1': 10, 'N22-WHBL2': 10, 'N22-WHBL3': 10, 'N50P-CT2': 10, 'N50P-CT4': 10, 'SA61-MBL': 10, 'SA61-MGR': 10, 'SA61-MHE': 10, 'SA61-MMB':
discounts = { 'N18-AB0': 10,
'N18-AB2': 10,
'N18-BL2': 10,
'N22-WHBL0': 10,
'N22-WHBL1': 10,
'N22-WHBL2': 10,
'N22-WHBL3': 10,
'N50P-CT2': 10,
'N50P-CT4': 10,
'SA61-MBL': 10,
'SA61-MGR': 10,
'SA61-MHE': 10,
'SA61-MMB': 10,
'SA61-MNA': 10,
'SA61-MPL': 10 }
然后,我使用lowdash
提取键值
specials = (req, res, next) ->
Promise.props
discounts: discounts
.then (result) ->
StyleIds = []
if result.discounts isnt null
discounts = result.discounts
StyleIds = _.forOwn discounts, (value, key) ->
styleId = key.split(/-/)[0]
styleId
如何返回一个styleid数组,以便获得唯一的值,例如'N18',N22',N50P',SA61']
非常感谢您的任何建议请尝试以下内容
var折扣={'N18-AB0':10,
“N18-AB2”:10,
“N18-BL2”:10,
“N22-WHBL0”:10,
“N22-WHBL1”:10,
“N22-WHBL2”:10,
“N22-WHBL3”:10,
“N50P-CT2”:10,
“N50P-CT4”:10,
“SA61-MBL”:10,
“SA61-MGR”:10,
“SA61-MHE”:10,
“SA61-MMB”:10,
“SA61-MNA”:10,
'SA61-MPL':10};
Array.prototype.getUnique=函数(){
变量u={},a=[];
对于(变量i=0,l=this.length;i
您可以使用和从折扣对象开始
var result = _(discounts)
.map(function(value, key) {
return key.split('-')[0];
})
.uniq()
.value();
console.log(result);
我不擅长CoffeeScript,但下面是一个在Javascript中使用lodash的示例
function getUniqueStyleIds(obj) {
return _.chain(obj)
.keys()
.map(function(key) { return key.split('-')[0]; })
.uniq()
.value();
}
我是这样得到的:
specials = (req, res, next) ->
Promise.props
discounts: discounts
.then (result) ->
if result.discounts isnt null
discounts = result.discounts
styles = _(result.discounts)
.keys()
.flatten()
.map( (c) -> c.split(/-/)[0] )
.unique()
.value()
然后styles返回['N18'、'N22'、'N50P'、'SA61']
快照,在我看到你的答案之前,我刚刚得到它,无论如何,谢谢。