0

このようなjsonがあります(開発ツールインスペクタからコピー)

salePurchases
  salesPeriods: Array[2]
    0: Object
      data: Array[5]
        0: Object
        1: Object
        2: Object
        3: Object
        4: Object
      period: "2011"
    1: Object
      data: Array[5]
        0: Object
        1: Object
        2: Object
        3: Object
        4: Object
      period: "2012"

  purchasePeriods: Array[2]
    0: Object
      data: Array[5]
        0: Object
        1: Object
        2: Object
        3: Object
        4: Object
      period: "2011"
    2: Object
      data: Array[5]
        0: Object
        1: Object
        2: Object
        3: Object
        4: Object
      period: "2012"

私はそれを引き離して、このような新しいオブジェクトを作成したいと思います -

OrderedByYears:
  2011:
    data: Array[10]
      0: Object // this should be the 1st item from the salesPeriods array with a period of 2011
      1: Object // this should be the 1st item from the purchasePeriods array with a period of 2011
      2: Object
      3: Object
      4: Object
      5: Object
      6: Object
      7: Object
      8: Object
      9: Object  // this should be the 5th item from the salesPeriods array with a period of 2011
      10: Object // this should be the 5th item from the purchasePeriods array with a period of 2011
  2012:
    data: Array[10]
      0: Object // this should be the 1st item from the salesPeriods array with a period of 2012
      1: Object // this should be the 1st item from the purchasePeriods array with a period of 2012
      2: Object
      3: Object
      4: Object
      5: Object
      6: Object
      7: Object
      8: Object
      9: Object // this should be the 5th item from the salesPeriods array with a period of 2012
      10: Object // this should be the 5th item from the purchasePeriods array with a period of 2012

キー「期間」に基づいて配置し、各配列を結合することを交互に行いますが、関連するオブジェクト salesPeriods と purchasePeriods を交互に使用します。

アンダースコアを使用すると、これは比較的簡単だと思います。それが簡単にできるかどうか誰でも知っていますか?関連する手順を説明していただければ幸いです。

4

2 に答える 2

2

あなたに合ったアプローチがあることを知っています。私はJavascript で関数型プログラミング ライブラリに取り組んできましたが、これがそのライブラリの問題を処理する方法です。

var weave = compose(flatten, zip);
reduce(function(obj, period) {
    obj[period.year] = {data: period.data};
    return obj;
}, {}, map(function(year) {
    var getMatch = find(function(period) {return period.period === year;});
    return {year: year, data: weave(getMatch(salePurchases.salesPeriods).data, 
                                    getMatch(salePurchases.purchasePeriods).data)};
}, pluck("period", salePurchases.salesPeriods)));

ほとんどの場合、パラメーターの順序は異なりますが、使用されるすべてのコア関数に相当するものがアンダースコアでも利用できます。したがって、アンダースコアを使用すると、次のように実行できます。

var weave = _.compose(_.flatten, _.zip);
_.reduce(_.map(_.pluck(salePurchases.salesPeriods, "period"), function(year) {
    var getMatch = function(period) {return period.period === year;}
    return {year: year, data: weave(_.find(salePurchases.salesPeriods, getMatch).data, 
                                    _.find(salePurchases.purchasePeriods, getMatch).data)};
}), function(obj, period) {
    obj[period.year] = {data: period.data};
    return obj;
}, {});

これは、 Ramdaまたは JSFiddle のUnderscoreを使用して実際に確認できます。

主なアイデアは、salesPeriods を取り除いた年のリストから始めるというものです。(これは、データが代表的であり、必要なすべての年が両方のリストに表示され、販売期間と購入期間の両方が包括的であると仮定しています。)次に、 を使用してmap、各年の販売期間と購入期間を織り交ぜます。年と混合データの配列を含むオブジェクトを返します。これを予想される出力に変えるためにreduce、新しいオブジェクトの関連する年のプロパティを、dataその混合データであるプロパティを持つオブジェクトに割り当てることによって、このリストを作成します。

この手法では、途中でエラーチェックをほとんど行うことはできませんが、非常にコンパクトです。

于 2013-07-23T19:14:32.373 に答える