我正在尝试根据属性合并数组中的对象,并将值添加到公共字段中。
我希望如果一个字段在数组中重新出现20次,那么公共字段应该包含20个对象所包含的所有值。附:价值观可能不同。
下面提到的回答会更清楚。
我的样本json是
[
{
"_id": "57ed13ada3a7403e1cf73d0f",
"id": 213,
"name": "Services",
"created_at": "2014-10-12 05:30:03.296711",
"updated_at": "2014-10-12 05:30:03.296711",
"priority": 0,
"tags_weight": 2
},
{
"_id": "57ed13ada3a7403e1cf73d0f",
"id": 213,
"name": "Services",
"created_at": "2014-10-12 05:30:03.296711",
"updated_at": "2014-10-12 05:30:03.296711",
"priority": 0,
"tags_weight": 35
},
{
"_id": "57ed13ada3a7403e1cf73fc5",
"id": 51,
"name": "Sports",
"created_at": "2014-10-11 03:22:13.640631",
"updated_at": "2015-02-16 15:22:19.389636",
"priority": 20,
"tags_weight": 2
},
{
"_id": "57ed13ada3a7403e1cf73fc5",
"id": 51,
"name": "Sports",
"created_at": "2014-10-11 03:22:13.640631",
"updated_at": "2015-02-16 15:22:19.389636",
"priority": 20,
"tags_weight": 90
},
{
"_id": "57ed13ada3a7403e1cf73c54",
"id": 11,
"name": "Free",
"created_at": "2014-10-11 03:22:11.783557",
"updated_at": "2014-10-11 03:22:11.783557",
"priority": 0,
"tags_weight": 2
},
{
"_id": "57ed13ada3a7403e1cf73c54",
"id": 11,
"name": "Free",
"created_at": "2014-10-11 03:22:11.783557",
"updated_at": "2014-10-11 03:22:11.783557",
"priority": 0,
"tags_weight": 90
},
{
"_id": "57ed13ada3a7403e1cf73c82",
"id": 67,
"name": "Events",
"created_at": "2014-10-11 03:22:13.817505",
"updated_at": "2014-10-11 03:22:13.817505",
"priority": 0,
"tags_weight": 2
}
]
我希望我的输出看起来像: -
[
{
"_id": "57ed13ada3a7403e1cf73d0f",
"id": 213,
"name": "Services",
"created_at": "2014-10-12 05:30:03.296711",
"updated_at": "2014-10-12 05:30:03.296711",
"priority": 0,
"tags_weight": 37
},
{
"_id": "57ed13ada3a7403e1cf73d0f",
"id": 213,
"name": "Services",
"created_at": "2014-10-12 05:30:03.296711",
"updated_at": "2014-10-12 05:30:03.296711",
"priority": 0,
"tags_weight": 92
},
{
"_id": "57ed13ada3a7403e1cf73fc5",
"id": 51,
"name": "Free",
"created_at": "2014-10-11 03:22:13.640631",
"updated_at": "2015-02-16 15:22:19.389636",
"priority": 20,
"tags_weight": 92
},
{
"_id": "57ed13ada3a7403e1cf73fc5",
"id": 51,
"name": "Events",
"created_at": "2014-10-11 03:22:13.640631",
"updated_at": "2015-02-16 15:22:19.389636",
"priority": 20,
"tags_weight": 2
}
]
可以观察到,在我想要的数组中,我已经使用通用名称合并了字段,并且我已经根据它在数组中的出现添加了每个唯一对象的tags_weight。