我从API获取这些数据:
let dataFromApi = [
{
// lots of other props
name: 'super cool place',
address: 'address of food place',
classification: 'food',
},
{
// lots of other props
name: 'name of tech place',
address: 'address of tech place',
classification: 'tech',
},
{
// lots of other props
name: 'name of clothes place',
address: 'address of clothes place',
classification: 'clothes',
},
{
// lots of other props
name: 'name of of another clothes place',
address: 'address of another clothes place',
classification: 'clothes',
},
{
// lots of other props
name: 'name of of another tech place',
address: 'address of another tech place',
classification: 'tech',
},
]
我想做的是创建一个新的对象数组,它有两件事:
-
一个叫做
classification
的props ; -
一个名为
establishments
的props ,是一个包含所有与分类props 匹配的机构的对象array.
意思是这样的:
let establishmentsArray = [
{
classification: 'food',
establishments: [
{
name: 'name of food place',
address: 'address of food place',
classification: 'food',
// rest of its props
},
// Lots of other food places places
],
},
{
classification: 'clothes',
establishments: [
{
name: 'name of clothes place',
address: 'address of clothes place',
classification: 'clothes',
// rest of its props
},
{
name: 'name of another clothes place',
address: 'address of another clothes place',
classification: 'clothes',
// rest of its props
},
// Lots of other clothes places
],
},
{
classification: 'tech',
establishments: [
{
name: 'name of tech place',
address: 'address of tech place',
classification: 'tech',
// rest of its props
},
{
name: 'name of another tech place',
address: 'address of another tech place',
classification: 'tech',
// rest of its props
},
// Lots of other tech places
],
},
]
提前谢谢你的帮助!