我是下划线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_id
和event_number
}在匹配name
的{{1}}时进入事件数组使用新数组的behavior
对象的对象。任何帮助表示赞赏。
答案 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"
}]
}]
}]