Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

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
            }
        ]
    }
]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...