我有一個大對象(1500+)和對象匹配其中一個屬性。所以可以減少對象的總數量,例如以獲得更好的ng-repeat性能。 我對AngularJS和JS本身很陌生,所以非常感謝幫助!AngularJS在屬性上組合數組中的對象
我的對象的陣列(簡化的)
myArray = [
{
"role": "user111",
"operation": "create",
"concept": "folder"
},
{
"role": "user111",
"operation": "create",
"concept": "task"
},
{
"role": "user111",
"operation": "delete",
"concept": "task"
},
{
"role": "user222",
"operation": "create",
"concept": "folder"
},
{
"role": "user222",
"operation": "create",
"concept": "task"
},
{
"role": "user222",
"operation": "delete",
"concept": "task"
}
]
期望的輸出
dreamArray = [
{
"role": "user111",
"operation": {
"create": ["folder", "task"],
"delete": ["task"]
}
},
{
"role": "user222",
"operation": {
"create": ["folder", "task"],
"delete": ["task"]
}
}
]
你總是收到排序的'role'財產的對象像在您的示例數據?如果是這樣,這是一個有價值的信息,並可以大大簡化代碼。 – Redu
@Redu其實我呢!這將如何簡化事情? – DTR9000
那麼你可以通過使用一個'.reduce()'操作在O(n)時間內做到這一點。請參閱下面的答案。 – Redu