最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to convert JSON Object into Javascript array - Stack Overflow

programmeradmin0浏览0评论

Linked to this question and this one also, I would like to know how to do the exact same thing but with a different type of JSON :

Here my JSON Object :

var myDb = {
    "04c85ccab52880": {
        "name": "name1",
        "firstname": "fname1",
        "societe": "soc1"
    },
    "04c95ccab52880": {
        "name": "name2",
        "firstname": "fname2",
        "societe": "soc2"
    },
    "048574220e2a80": {
        "name": "name3",
        "firstname": "fname3",
        "societe": "soc3"
     }
};

My problem is that my JSON is different from their JSON, actually I have an array of array in mine. So how to convert this into a Javascript array ?

var arr = [];

$.each( myDb, function( key, value ) {
    arr.push( value );    
});

console.log(user_list);

This kind of script seems to return my 3 different objects but where are my uid from JSON ? How can I get them ? Because now my keys are 0,1,2 and no longer my uid.

Any ideas ? Thanks

Linked to this question and this one also, I would like to know how to do the exact same thing but with a different type of JSON :

Here my JSON Object :

var myDb = {
    "04c85ccab52880": {
        "name": "name1",
        "firstname": "fname1",
        "societe": "soc1"
    },
    "04c95ccab52880": {
        "name": "name2",
        "firstname": "fname2",
        "societe": "soc2"
    },
    "048574220e2a80": {
        "name": "name3",
        "firstname": "fname3",
        "societe": "soc3"
     }
};

My problem is that my JSON is different from their JSON, actually I have an array of array in mine. So how to convert this into a Javascript array ?

var arr = [];

$.each( myDb, function( key, value ) {
    arr.push( value );    
});

console.log(user_list);

This kind of script seems to return my 3 different objects but where are my uid from JSON ? How can I get them ? Because now my keys are 0,1,2 and no longer my uid.

Any ideas ? Thanks

Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked Jun 19, 2014 at 9:49 hacks4lifehacks4life 6935 gold badges17 silver badges39 bronze badges 1
  • possible duplicate of How to parse JSON in JavaScript – Ashish Ratan Commented Jun 19, 2014 at 10:31
Add a ment  | 

2 Answers 2

Reset to default 6

A working JSFIDDLE

JS code:

//var arr = [];
var myDb = {
    "04c85ccab52880": {
        "name": "name1",
        "firstname": "fname1",
        "societe": "soc1"
    },
    "04c95ccab52880": {
        "name": "name2",
        "firstname": "fname2",
        "societe": "soc2"
    },
    "048574220e2a80": {
        "name": "name3",
        "firstname": "fname3",
        "societe": "soc3"
     }
};

$.each( myDb, function( key, value ) {
    //arr.push( value );    
    console.log("key => "+key);//will output: 04c85ccab52880 and all such
    $.each( value, function( ky, val ) {
        console.log('ky => '+ky);//will output: name, firstname, societe
        console.log('val => '+val);//will output: name1, fname1, soc1
    });    
});

if you want to convert them, with key. I suggest something like below

var mapped = Object.keys( myDb  ).map(function( uid ){
    return (myDb[uid ].uid = uid ) && myDb[uid ];
});

for value in your array look like, example mapped[0] has value :

{
    "name": "name1",
    "firstname": "fname1",
    "societe": "soc1",
    "uid": "04c85ccab52880"
}
发布评论

评论列表(0)

  1. 暂无评论