JSON Array
- In JSON, array can store only valid JSON data types as value (string, number, array, object, boolean or null)
- turned_in_notArray can be divided in two ways
- Simple Array
- Nested Array
Simple Array
[value1,value2,value3,....,valueN]
Example
[75,65,89,95]
In json just put key value as an array
Example
{"class":6,"marks":[75,65,89,95]}
Here above, keyName "marks" is having an array as its value
Hope you remember how to access an object value, if not then check JSON Object
Now, think above example as:
var obj = {"class":6,"marks":someValue};
So, to access keyName "marks" value we have 2 optionsobj.marks;
ORobj["marks"];
- Remember! if you use the above syntax for an array then it will print all the values declared inside an array (but object value inside an array print as object)
Now, you can access specific array value via its index
obj.marks[index];
ORobj["marks"][index];
*using dot (.)
Syntax: obj.keyName[index]
var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html(obj.marks[2]);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html(obj.marks[2]);
</script>
</body>
</html>
aaa
*using bracket ([ ])
Syntax: obj.["keyName"][index]
var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html(obj["marks"][1]);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html(obj["marks"][1]);
</script>
</body>
</html>
/*Syntax-To change an array index value*/
obj.keyName[index]=newValue;
ORobj["keyName"][index]=newValue;
var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html("Before : "+ obj.marks[1]);
obj.marks[1]=99;
$("p").append("<br>After Changing : "+ obj.marks[1]);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html("Before : "+ obj.marks[1]);
obj.marks[1]=99;
$("p").append("<br/>After Changing : "+ obj.marks[1]);
</script>
</body>
</html>
/*Syntax-To delete an array index value*/
delete obj.keyName[index];
ORdelete obj["keyName"][index];
var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html("Before : "+ obj.marks[1]);
delete obj.marks[1];
$("p").append("<br>After deleting : "+ obj.marks[1]);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html("Before : "+ obj.marks[1]);
delete obj.marks[1];
$("p").append("<br/>After deleting : "+ obj.marks[1]);
</script>
</body>
</html>
Nested Array
Array inside an array
Example
[0,[1,2,3],4,5]
In Json{ "number":[0,[1,2,3],4,5] }
Here, above keyName "number" is having nested array
var obj = { "number":[0,[1,2,3],4,5] };
$("p").html("index[1] : "+ obj.number[1]+
"<br> index[1][1] : "+ obj.number[1][1]);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = { "number":[0,[1,2,3],4,5] };
$("p").html("index[1] : "+ obj.number[1]+
"<br> index[1][1] : "+ obj.number[1][1]);
</script>
</body>
</html>
To access more inside nested array
/*Syntax-In general way*/
obj.keyName[index][nextIndex1]...[nextIndexN];
ORobj.["keyName"][index][nextIndex1]...[nextIndexN];
To change nested array value
/*Syntax-In general way*/
obj.keyName[index][nextIndex1]...[nextIndexN]= newValue;
ORobj.["keyName"][index][nextIndex1]...[nextIndexN]= newValue;
To delete nested array value
/*Syntax-In general way*/
delete obj.keyName[index][nextIndex1]...[nextIndexN];
ORdelete obj.["keyName"][index][nextIndex1]...[nextIndexN];