-3

I have a object like this one. How can i convert it to Array in JavaScript.

I'm try so hard, but it doesn't work, i can not alert or console.log it.

I have this object below

{
    "2017": {
        "08": [{
            "id": "22",
            "pass": "temp1"
        }, {
            "id": "23",
            "pass": "af",
        }],

        "09": [{
            "id": "25",
            "pass": "zx"

        }]
    },

    "2018": {
        "08": [{
            "id": "24",
            "pass": "gre"
        }]
    }
}

And this is the array i want it to be in JavaScript

Array
(
    [2017] => Array
        (
            [08] => Array
                (
                    [0] => Array
                        (
                            [id] => 22
                            [pass] => temp1
                        )

                    [1] => Array
                        (
                            [id] => 23
                            [pass] => af
                        )

                )

            [09] => Array
                (
                    [0] => Array
                        (
                            [id] => 25
                            [pass] => zx

                        )

                )

        )

    [2018] => Array
        (
            [08] => Array
                (
                    [0] => Array
                        (
                            [id] => 24
                            [pass] => gre
                        )

                )

        )

)

Thank you very much for answering

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

3 Answers3

0

For the very first, I would like to tell you that this isn't a mere JavaScript object, it's a list of JSON objects.

Get this list in some variable and then use it like given in below link.

Loop through JSON object List

0

You can use underscore.js to do this:

_(obj).each(function(elem, key){
   arr.push(elem[0]);
});
Nitesh Rana
  • 512
  • 1
  • 7
  • 20
0

If all you want to achieve is to loop through the object you don't have to convert it to an array. In JavaScript, there is for...in loop which will be perfect in this case. Use it like this:

var myobj = {
    key1: "val1",
    key2: "val2",
    key3: "val3",
}

for(var key in myobj){
    var value = myobj[key];

    console.log(key, value);
}

Visit this link to read more about for...in loop

Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38