# 资料处理牛人请进, js分类资料问题(lodash)

Chienwei 2017年12月07日提问
0
``````{
"category": {
"sum": 34,
"long_1": "odd",
"long_2": "odd",
"long_3": "even",
"long_4": "odd",
"long_5": "odd",
"size_1": "bigger",
"size_2": "bigger",
"size_3": "bigger",
"size_4": "bigger",
"size_5": "bigger",
"GD_1_5": "G",
"sum_long": "even",
"sum_size": "bigger",
"weird_1_3": "straight",
"weird_2_4": "half_straight",
"weird_3_5": "pair"
},
"str": "7,9,8,5,5"
},``````

``````[
0:{
long: odd,
size: bigger,
num: 7,
},
1:{
long: odd,
size: bigger,
num: 9,
},
2:{
long: even,
size: bigger,
num: 8,
},
3:{
long: odd,
size: bigger,
num: 8,
},
4:{
long: odd,
size: bigger,
num: 5,
}
]``````

## 2个回答

1

``````var data = {
"category": {
"sum": 34,
"long_1": "odd",
"long_2": "odd",
"long_3": "even",
"long_4": "odd",
"long_5": "odd",
"size_1": "bigger",
"size_2": "bigger",
"size_3": "bigger",
"size_4": "bigger",
"size_5": "bigger",
"GD_1_5": "G",
"sum_long": "even",
"sum_size": "bigger",
"weird_1_3": "straight",
"weird_2_4": "half_straight",
"weird_3_5": "pair"
},
"str": "7,9,8,5,5"
}

var result = data.str.match(/\d+/g).map((num, i) => ({
long: data.category[`long_\${i + 1}`],
size: data.category[`size_\${i+1}`],
num
}))

console.log(result)``````

1
``````const obj = JSON.parse(str)
const {category, str} = obj

let result = str.split(',').map((v, i) =>({
long: category[`long_\${i+1}`],
size: category[`size_\${i+1}`],
num: v
}))``````