-1

i have a json with some dates that looks like this for example

jsonEvents = [
    {'event_name':'event1','event_start':'2013-10-23','event_stop':'2013-10-29'},
    {'event_name':'event2','event_start':'2013-10-23','event_stop':'2013-10-25'}
];

i want to use underscoreJS to cut this events into half (just to make the idea more simple). What i want as output would look like this.

jsonEvents = [
    {'event_name':'event1','event_start':'2013-10-23','event_stop':'2013-10-26'},
    {'event_name':'event1','event_start':'2013-10-26','event_stop':'2013-10-29'},
    {'event_name':'event2','event_start':'2013-10-23','event_stop':'2013-10-24'},
    {'event_name':'event2','event_start':'2013-10-24','event_stop':'2013-10-25'}
];

I hope i can do this in a simple way, i am working on an angularJS project and i hope underscoreJS class can do this. Thank you very much.

4

1 に答える 1

0

concatMapこれには、アンダースコアが提供していない何らかの種類が必要です。これを行うjQueryを使用するか$.map、自分で構築することができます:

var concatMap = _.compose(_.flatten, _.map);

これで、配列を反復処理して、アイテムごとに 2 つのイベントの配列を返すことができます。

function createEvent(name, start, end) {
    return {
        "event_name": name,
        "event_start": format(start),
        "event_stop": format(end)
    };
}
jsonEvents = concatMap(jsonEvents, function (event) {
    var from = new Date(event.event_start),
        to = new Date(event.event_stop),
        mid = new Date( (+startDate + stopDate) / 2 );
    return [
        createEvent(event.event_name, from, mid),
        createEvent(event.event_name, mid, to)
    ];
});
于 2013-10-24T11:21:59.620 に答える