【发布时间】:2018-10-07 07:29:15
【问题描述】:
我正在尝试动态地根据属性值将对象数组拆分为组。
这是一个输入示例:
`input = [
{"name": "john", "location": "first"},
{"name": "steve", "location": "first"},
{"name": "paul", "location": "another"},
{"name": "tony", "location": "random"},
{"name": "ant", "location": "random"}
]`
以及所需的输出:
`solution(input, location) = [
first: [{"name": "john", "location": "first"},
{"name": "steve", "location": "first"}],
another: [{"name": "paul", "location": "another"}],
random: [{"name": "tony", "location": "random"},
{"name": "ant", "location": "random"}]
]`
我不知道 location 可以是什么值(但我知道键名)
我试图避免使用任何外部库, (这是在一个 Angular 5 项目中) 但如果它使事情变得更容易,那么我并不反对。
提前致谢
【问题讨论】:
-
使用
.concat(arr, arr2) -
@Karabah 你确定你读对了这个问题吗?
标签: javascript typescript