js数组转换问题

  {"data": {
    "char": [
        [
            [],
            [29, "新", "心", "char_2"],
            []
        ]
    ],
    "miss": [
        [
            [29, "新", "心", "miss_1"],
            [],
            [29, "新", "心", "miss_2"],
        ]
    ],
    "order": [
        []
    ]
}}

转换为

 {"event_list": [
    {
        "big": {
            "type": "char"
        },
        "small": [
            {
                "type": "",
                "correct": "",
                "text": "",
                "pos": "",
                "index": 0
            },
            {
                "type": "char_2",
                "correct": "心",
                "text": "新",
                "pos": 10,
                "index": 1
            },
            {
                "type": "",
                "correct": "",
                "text": "",
                "pos": "",
                "index": 2
            },
        ]
    },
    {
        "big": {
            "type": "miss"
        },
        "small": [
            {
                "type": "miss_1",
                "correct": "心",
                "text": "新",
                "pos": 20,
                "index": 0
            },
            {
                "type": "",
                "correct": "",
                "text": "",
                "pos": "",
                "index": 1
            },
            {
                "type": "miss_2",
                "correct": "心",
                "text": "新",
                "pos": 10,
                "index": 2
            }
        ]
    }
]}
阅读 1.2k
1 个回答

不知道你pos字段怎么转换的,权且当做那个29吧:

Object.keys(data)
    .map(key => {
        return {
            big: {
                type: key
            },
            small: data[key][0].map((ele, inx) => {
                return {
                    type: ele[3] || '',
                    corrent: ele[2] || '',
                    text: ele[1] || '',
                    pos: ele[0] || '',
                    index: inx
                }
            })
        }
    })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题