如何将json数组对象从firebase服务器json转换为javascript数组?

时间:2017-07-23 17:54:12

标签: javascript json firebase firebase-realtime-database

这是我从Firebase获得的格式

{
    "-KpigwmSqAYFFaWgHYZ5": {
        "name": "chomroeun",
        "password": "123",
        "phone": "093909157",
        "team": "Ekerk"
    },
    "-KpihLkWatPmYJBlVjCR": {
        "name": "hello",
        "password": "123",
        "phone": "322344",
        "team": "eer"
    },
    "-KpihrxRw_L5Z2f5Jceb": {
        "name": "visal",
        "offline_image": {
            "-KpjPf3fO8Vz1rVu-lvl": "content://com.animalmama.innov8_dealer/my_images/id_card_PP00003_20170723_192720_1055007560.jpg",
            "-KpjPjtIGjHoL_bvKF5x": "content://com.animalmama.innov8_dealer/my_images/id_card_PP00003_20170723_192720_1055007560.jpg",
            "-KpjPjtJcTAIL3PZTjX9": "content://com.animalmama.innov8_dealer/my_images/id_card_PP00003_20170723_192739_473034014.jpg"
        },
        "password": "1234",
        "phone": "069331000",
        "team": "k01"
    },
    "-KpiiPhpXxDHZd4JRYNw": {
        "name": "Hongda",
        "password": "123",
        "phone": "098776688",
        "team": "Hog"
    },
    "-KpiiX8Xf9fWHAheIJlm": {
        "name": "Hongda",
        "password": "123",
        "phone": "098776688",
        "team": "Hog"
    },
    "-KpiiixW0jRegBk6-CL2": {
        "name": "TT",
        "password": "123",
        "phone": "059544",
        "team": "dd"
    }
}

但我想要的格式是什么

{ "aaData": [
        [ "1", "Armand", "Warren", "56045", "Taiwan, Province of China" ],
        [ "2", "Xenos", "Salas", "71090", "Liberia" ],
        [ "3", "Virginia", "Whitaker", "62723", "Nicaragua" ],
        [ "4", "Kato", "Patrick", "97662", "Palau" ],
        [ "5", "Penelope", "Hensley", "76634", "Greenland" ],
        [ "6", "Georgia", "Erickson", "81358", "Bolivia" ]
    ]
}

1 个答案:

答案 0 :(得分:0)

当你在使用firebase时,数组不是你的朋友。 但是,如果要将对象转换为数组,可以使用map函数通过根对象道具迭代并返回数组

 Object.keys(asd).map(function(a,i){
 return[i,asd[a].name,asd[a].password]
 })

这是plunker