根据示例你给:
var myDB = [{ "xCounter": "positive", "day": "first" }, { "xCounter": "positive", "day": "second" }];
使用groupBy
方法,你会得到以下结果在days
变量:
{
first: [{ "xCounter": "positive", "day": "first" }],
second: [{ "xCounter": "positive", "day": "second" }]
}
要经过这样的结果,你可以使用以下片段:
for (var day in days) {
if (days.hasOwnProperty(day)) {
// do something with 'day' (which will take tha values 'first' and 'second')
// and 'days[day]' (which is an array of {xCounter: X, day: Y} objects)
}
}
PS:我会建议(https://lodash.com/docs/4.16.1#groupBy)作为Lodash发展更积极,但这是我的意见;)
嗨,你可以提供一个myDB变量内的例子吗? – Brunt
Hi @Brunt,这里是一个例子:myDB = [{“xCounter”:“positive”,“day”:“first”},{“xCounter”:“positive”,“day”:“second”}] –