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?
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?
0
is not a valid identifier, so you need to use index notation:
data[0].code
Seems weird to have keys as numbers, use bracket notation.
data["0"].name
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.