1

There is a big object in jquery script, and I want extract it (get variables. I figure that the need to use ".". Example:

data.0.name

But in my case this not work. Attached Images with examples. How i can get "code" variable?enter image description here

user319854
  • 3,980
  • 14
  • 42
  • 45
  • What if you replace your object with simple array (`[]`)? It looks strange to keep numeric properties in object. – VisioN Apr 18 '13 at 17:10

3 Answers3

5

0 is not a valid identifier, so you need to use index notation:

data[0].code
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

Seems weird to have keys as numbers, use bracket notation.

data["0"].name
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

I'll reply in-line to @SLaks, he is absolutely correct.

If i am not wrong then you have your data something like:-

        var data = [
            {
                "code": "Lorem",
                "created": "2012-01-01"
            },
            {
                "code": "Lorem",
                "created": "2012-01-02"
            },
            {
                "code": "Lorem",
                "created": "2012-01-03"
            }
        ];

Now if you need to access the data, you can possibly try two alternatives :-

1st using .each

//If your using .each for Array
        $.each(data, function (index, value) {
            console.log("1st Param= " + value.code + "| 2nd Param= " + value.created);
        });

2nd :- If you manually want to access using the index then you could try :-

//If you manualy want to access:
for (var i = 0; i < data.length; i++) {
    console.log("1st Param= " + data[i].code + "| 2nd Param= " + data[i].created);
}

Just for reference you could copy and paste the HTML file:-

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            var data = [
                {
                    "code": "Lorem",
                    "created": "2012-01-01"
                },
                {
                    "code": "Lorem",
                    "created": "2012-01-02"
                },
                {
                    "code": "Lorem",
                    "created": "2012-01-03"
                }
            ];
            console.log(data);
            //If your using .each for Array
            $.each(data, function (index, value) {
                console.log("1st Param= " + value.code + "| 2nd Param= " + value.created);
            });
            //If you manualy want to access:
            console.log("----------");
            for (var i = 0; i < data.length; i++) {
                console.log("1st Param= " + data[i].code + "| 2nd Param= " + data[i].created);
            }


        });

    </script>
</head>
<body>

</body>
</html>

[Update] Didn't notice @Palash Mondal reply, which is what i wanted to convey. Which seems correct to me.

Shubh
  • 6,693
  • 9
  • 48
  • 83