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

{
            "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.4k
2 个回答

不知道你要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)

clipboard.png

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
}))
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题