Underscore.js:映射要删除的对象数组并组合重复数据

时间:2013-11-09 00:42:49

标签: angularjs underscore.js

我是下划线js库的新手,我正在尝试重组我的一些数据,以便更好地满足我的应用程序需求。我的API中包含以下数据:

{
    "id": "123456",
    "first_name": "John",
    "last_name": "Doe",
    "behaviours": [
        {
            "id": 17,
            "name": "Following Too Closely",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4482",
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4482",
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4481",
        }
    ]
},
{
    "id": "123456",
    "first_name": "Jane",
    "last_name": "Doe",
    "behaviours": [
        {
            "id": 17,
            "name": "Following Too Closely",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4482",
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4482",
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "event_id": "01234-01234-01234",
            "event_number": "ABG4481",
        }
    ]
}

我想映射数据,以便我可以提取唯一的behaviour名称,并创建一个behaviours分组的新数组。所以我的数据看起来像这样:

{
    "id": "123456",
    "first_name": "John",
    "last_name": "Doe",
    "behaviours": [
        {
            "id": 17
            "name": "Following Too Closely",
            "events": [
                { 
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4482" 
                }
            ]
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "events": [
                {
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4482"
                },
                {
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4481"
                }
            ]
        }
    ]
},
{
    "id": "123456",
    "first_name": "Jane",
    "last_name": "Doe",
    "behaviours": [
        {
            "id": 17
            "name": "Following Too Closely",
            "events": [
                { 
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4482" 
                }
            ]
        },
        {
            "id": 19,
            "name": "Not Looking Far Ahead",
            "events": [
                {
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4482"
                },
                {
                    "event_id": "01234-01234-01234",
                    "event_number": "ABG4481"
                }
            ]
        }
    ]
}

我目前得到的代码不是很多,我坚持这个问题而且我不确定如何有效地解决这个问题。

for (var i = $scope.eventData.drivers.length - 1; i >= 0; i--) {

    // Create an array containing only the unique behavior names
    var behaviourArray = _.uniq(_.pluck($scope.eventData.drivers[i].behaviours, 'name'));

    // Create a property containing a joined array of the behavior names
    $scope.eventData.drivers[i].behaviourNames = behaviourArray.join(', ');

    behaviourArray = _.map(behaviourArray, function(name) {
        return {
            'name': name,
            'events': []
        };
    });
};

我不确定如何push事件对象{event_idevent_number}在匹配name的{​​{1}}时进入事件数组使用新数组的behavior对象的对象。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

以下内容生成您需要数据的确切结构。

这是你最初的事情

var data = [{
  "id": "123456",
  "first_name": "John",
  "last_name": "Doe",
  "behaviours": [{
    "id": 17,
    "name": "Following Too Closely",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4482",
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4482",
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4481",
  }]
}, {
  "id": "123456",
  "first_name": "Jane",
  "last_name": "Doe",
  "behaviours": [{
    "id": 17,
    "name": "Following Too Closely",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4482",
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4482",
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "event_id": "01234-01234-01234",
    "event_number": "ABG4481",
  }]
}];
奶油!!!

_.each(data, function (driver) {
  driver.behaviours = _(driver.behaviours)
    .chain()
    .groupBy(function (x) {
      return x.id;
    })
    .map(function (g) {
      return {
        id: g[0].id,
        name: g[0].name,
        events: _.map(g, function (x) {
          return {
            event_id: x.event_id,
            event_number: x.event_number
          }
        })
      };
    })
    .value();
});

做一个日志

console.log(JSON.stringify(data));

以下是输出

[{
  "id": "123456",
  "first_name": "John",
  "last_name": "Doe",
  "behaviours": [{
    "id": 17,
    "name": "Following Too Closely",
    "events": [{
      "event_id": "01234-01234-01234",
      "event_number": "ABG4482"
    }]
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "events": [{
      "event_id ": "01234-01234-01234",
      "event_number": "ABG4482"
    }, {
      "event_id": "01234-01234-01234",
      "event_number": "ABG4481"
    }]
  }]
}, {
  "id": "123 456",
  "first_name": "Jane",
  "last_name": "Doe",
  "behaviours": [{
    "id": 17,
    "name": "Following Too Closely",
    "events": [{
      "event_id": "01234-01234-01234",
      "event_number": "ABG4482"
    }]
  }, {
    "id": 19,
    "name": "Not Looking Far Ahead",
    "events": [{
      "event_id": "01234-0123 4-01234",
      "event_number": "ABG4482"
    }, {
      "event_id": "01234-01234-01234",
      "event_number": "ABG4481"
    }]
  }]
}]