Nach dem Beispiel geben Sie:
var myDB = [{ "xCounter": "positive", "day": "first" }, { "xCounter": "positive", "day": "second" }];
Mit der groupBy
Methode, werden Sie das folgende Ergebnis in der days
Variable erhalten:
{
first: [{ "xCounter": "positive", "day": "first" }],
second: [{ "xCounter": "positive", "day": "second" }]
}
Zu diesem Ergebnis durchlaufen, können Sie Verwenden Sie das folgende Codefragment:
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: Ich würde vorschlagen t Lodash anstelle von Underscore verwenden, wenn möglich (https://lodash.com/docs/4.16.1#groupBy) als Lodash Entwicklung ist aktiver, aber das ist meiner Meinung nach;)
Hallo, könnten Sie ein Beispiel dafür geben, was in der Variable myDB ist? – Brunt
Hallo @Brunt, hier ist ein Beispiel: myDB = [{"xCounter": "positiv", "Tag": "zuerst"}, {"xCounter": "positiv", "Tag": "Sekunde"}]; –