js 数组转换问题求教(被生活所折弯)还剩一点未完善!请指教

  {"data": {
    "char": [
        [
            [51, "逸", "毅", "char_2"],
            [152, "参", "产", "char_2"],
        ],
        []
        [
            [29, "情", "清", "char_2"],
        ]
    ],
    "miss": [
        [
            [291, "新", "心", "miss_1"],
            [29, "新", "心", "miss_2"],
        ],
        []
    ],
    "order": [
        []
    ]
}}

如何转换为

    {
        "big": {
            "type": "char"
        },
        "small": [
            {
                "type": "char_2",
                "correct": "毅",
                "text": "逸",
                "pos": "51",
                "index": 0
            },
            {
                "type": "char_2",
                "correct": "参",
                "text": "产",
                "pos": "152",
                "index": 0
            },
            {
                "type": "",
                "correct": "",
                "text": "",
                "pos": "",
                "index": 1
            },
            {
                "type": "char_2",
                "correct": "清",
                "text": "情",
                "pos": "29",
                "index": 2
            },
        ]
    },
    {
        "big": {
            "type": "miss"
        },
        "small": [
            {
                "type": "miss_1",
                "correct": "心",
                "text": "新",
                "pos": 291,
                "index": 0
            },
            {
                "type": "miss_1",
                "correct": "心",
                "text": "新",
                "pos": 29,
                "index": 0
            },
            {
                "type": "",
                "correct": "",
                "text": "",
                "pos": "",
                "index": 1
            },
        ]
    }
]}

已解还有一点未完善关于[]部分

convert(data) {  
         return Object.entries(data)  
           .map(([type,list]) =>{  
             const big = {type}  
             const small = list.map((item,index)=>{  
                 return item.map((it) =>{  
                 const [pos, text, correct, type] = it;   
                 return { pos, text, correct, type, index };  
             })  
             }).reduce((total, current) => {  
             return [...total, ...current]  
             }, [])  
             return {big,small}   
             })  
 }, 
 const reslut = {  
 event_list: this.convert(res.data.data)  
 }
 
阅读 1.3k
1 个回答

应该是你要的效果吧:

image.png

let a = {
    "data": {
        "char": [
            [
                [51, "逸", "毅", "char_2"],
                [152, "参", "产", "char_2"],
            ],
            [
                [29, "情", "清", "char_2"],
            ]
        ],
        "miss": [
            [
                [291, "新", "心", "miss_1"],
                [29, "新", "心", "miss_2"],
            ],
        ]
    }
};

function convert(json) {
    if(!Object.keys(json).length) {
        return;
    }

    let event_list = [];
    let aArr = Object.keys(json.data);
    let smallData = [];
       
    aArr.forEach((v, k) => {
        let item = {
            big: {
                type: v
            },
            small: []
        };

        smallData[k] = [];
        json.data[v].flat().forEach((sv, sk) => {
            let smallItem = [];
            smallItem['pos'] = sv[0];
            smallItem['correct'] = sv[1];
            smallItem['text'] = sv[2];
            smallItem['type'] = sv[3];
            smallItem['index'] = sk;
            smallData[k].push(smallItem);
        });

        item.small.push(...smallData[k]);

        event_list.push(item);
    });
    
    return { event_list };
}
console.log(convert(a));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏