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"
        },

想用lodash 把它变成这样

[
  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

不知道你要lodash何用?

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)

0
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
}))

撰写答案