-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

12
  • 2
    What is your desired array? Commented Sep 7, 2017 at 5:57
  • Like the object but i want to have a key to loop it Commented Sep 7, 2017 at 5:58
  • You can use Object.keys() or Object.values() Commented Sep 7, 2017 at 5:59
  • 2
    Update the question with your desired array, then it would be easier for other user to solve the problem Commented Sep 7, 2017 at 6:01
  • 1
    The array you posted is PHP associative array. Which in JavaScript is represented as Object which you already have. See my answer to know how to iterate over it Commented Sep 7, 2017 at 6:15

3 Answers 3

0

You can use underscore.js to do this:

_(obj).each(function(elem, key){
   arr.push(elem[0]);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Why underscore.js? There is easy pure JS solution for this. There is no need to load any library for that
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

3 Comments

But it is a JS object.
You're right. This is a object, but concern is, what the object contains. It contains a list of objects.
As per the code snippet in question, the JS object contains a JSON list of objects.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.