怎么把返回的数据按照某个字段
def index
json_str = Groupall.to_json
render :json => json_str, status => '200'
end
[
{
"id": 3,
"group": "分组0",
"name": "测试2",
"active": "ceshi",
"created_at": "2017-05-02T02:02:37.283Z",
"updated_at": "2017-05-02T02:02:37.283Z"
},
{
"id": 4,
"group": "分组1",
"name": "测试3",
"active": "ceshi",
"created_at": "2017-05-02T02:02:37.287Z",
"updated_at": "2017-05-02T02:02:37.287Z"
},
{
"id": 5,
"group": "分组0",
"name": "测试4",
"active": "ceshi2",
"created_at": "2017-05-02T02:02:37.290Z",
"updated_at": "2017-05-02T02:02:37.290Z"
}
]
想要转换成 就是按照 group 字段的值进行分组,然后再 children 里面把 NAME 相同的设为一个对象,把 active,不同的字段合并到一个数组里
[
{
"group": "分组0",
"children":[
{“name”:"测试2" ,"active": ["ceshi2"]},
{“name”:"测试4"}
]
},
{
"group": "分组1",
"children":[
{“name”:测试3,"active": ["ceshi2"]}
]
},
]