0

I have json that looks like this (copied from dev tools inspector)

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"

I want to pull it apart and make it creat a new object that looks like this -

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

I'm basing the arrangement by the key 'period' and then alternating between joining each array together but alternating between the relavant object salesPeriods and purchasePeriods.

I'm sure this is relatively simple using underscore. Anyone know if it's an easy thing to do? Explaining the steps involved would be much appreciated.

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)));

Underscore 中也提供了所有使用的核心函数的等效项,尽管在大多数情况下,参数顺序不同。所以使用下划线你可以这样做:

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;
}, {});

您可以在RamdaJSFiddle上的 Underscore中看到这一点。

主要的想法是,我会从一个年份列表开始,从 salesPeriods 中剔除。(这是假设您的数据具有代表性,并且所有需要的年份都将出现在两个列表中,并且销售期间和购买期间都是全面的。)然后,使用map,我将每年的销售期间和购买期间编织在一起,返回一个带有年份的对象和一组混合数据。为了将其转换为您的预期输出,我将reduce通过将新对象的相关年份属性分配给其data属性是混合数据的对象来列出此列表。

这种技术允许在此过程中进行很少的错误检查,但非常紧凑。

于 2013-07-23T19:14:32.373 回答
0

我没有使用 underscore.js,但它看起来很好地映射到一般概念。这是您想要的一般方法:

  1. concatsalesPeriods 和 purchasePeriods 一起成为一个列表。
  2. groupBy每个对象中的period值。
  3. map将分组结果放入您想要的对象形状中。

sort根据您的目标,您可能希望在其中添加一些声明。

于 2013-07-23T17:22:21.797 回答