JSON Array of Numbers
A JSON array of numbers is a collection of numeric values enclosed in square brackets []. The numbers can be integers or floating-point values.
For example :
{
"scores": [
85,
92.5,
78,
90,
88.7
]
}
In this example, we have a JSON object with a key "scores" & its value is an array of numbers. The array contains five numeric values: 85, 92.5, 78, 90, & 88.7.
We can access individual elements of the array using their index, just like we did with the string array. For example, to access the third element of the "scores" array, we can use:
console.log(scores[2]);
Output:
78
We can also perform mathematical operations on the elements of the array. For example, to calculate the average score:
const sum = scores.reduce((acc, curr) => acc + curr, 0);
const average = sum / scores.length;
console.log(average);
Output
86.84
JSON Array of Booleans
A JSON array of booleans is a collection of boolean values (true or false) enclosed in square brackets [].
For example:
{
"completed": [
true,
false,
true,
true,
false
]
}
In this example, we have a JSON object with a key "completed" & its value is an array of booleans. The array contains five boolean values: true, false, true, true, & false.
We can access individual elements of the array using their index. For example, to access the second element of the "completed" array, we can use:
console.log(completed[1]);
Output
false
We can also use boolean arrays to perform logical operations or to filter data based on certain conditions. For example, to count the number of completed tasks:
const completedCount = completed.filter(Boolean).length;
console.log(completedCount);
Output
3
In this code, we use the `filter()` method with the `Boolean` function to create a new array containing only the `true` values. Then, we get the length of the resulting array to count the number of completed tasks.
JSON Array of Objects
A JSON array of objects is a collection of JSON objects enclosed in square brackets []. Each object in the array is defined using curly braces {} & can have multiple key-value pairs.
For example:
{
"students": [
{
"name": "Rahul",
"age": 20,
"major": "Computer Science"
},
{
"name": "Rinki",
"age": 21,
"major": "Electrical Engineering"
},
{
"name": "Harsh",
"age": 19,
"major": "Business Administration"
}
]
}
In this example, we have a JSON object with a key "students" & its value is an array of objects. Each object in the array represents a student with properties like "name", "age", & "major".
We can access individual objects in the array using their index & then access the properties of each object using dot notation or bracket notation. For example:
console.log(students[0].name);
console.log(students[1]["age"]);
Output
Rahul
21
We can also iterate over the array of objects using loops or array methods like `forEach()`, `map()`, or `filter()` to perform operations on each object. For example, to get an array of student names:
const names = students.map(student => student.name);
console.log(names);
Output
["Rahul", "Rinki", "Harsh"]
JSON Array of Arrays OR JSON Multidimensional Array
A JSON array of arrays, also known as a multidimensional array, is an array that contains other arrays as its elements. Each inner array can have its own elements of any data type, including strings, numbers, booleans, objects, or even other arrays.
For example:
{
"matrix": [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
}
In this example, we have a JSON object with a key "matrix" & its value is an array of arrays. The outer array contains three inner arrays, each representing a row of the matrix. Each inner array contains three numeric values.
We can access elements of a multidimensional array using multiple indexes. The first index represents the position of the outer array, & the second index represents the position within the inner array. For example:
console.log(matrix[0][1]);
console.log(matrix[1][2]);
Output
2
6
We can iterate over a multidimensional array using nested loops. For example, to print each element of the matrix:
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
console.log(matrix[i][j]);
}
}
This code uses two nested loops to iterate over each row (outer array) & each column (inner array) of the matrix, printing each element.
Multidimensional arrays are useful for representing grid-like structures, such as matrices, game boards, or tabular data.
Frequently Asked Questions
What is the difference between a JSON array & a regular JavaScript array?
JSON arrays & JavaScript arrays are similar in structure, but JSON arrays are represented as strings & follow the JSON syntax rules. JavaScript arrays are objects & have built-in methods & properties that JSON arrays do not have. JSON arrays are primarily used for data transmission & storage, while JavaScript arrays are used for manipulating data within a program.
Can JSON arrays contain elements of different data types?
Yes, JSON arrays can contain elements of different data types, such as strings, numbers, booleans, objects, or even other arrays. Each element in the array can be of a different type, allowing for flexible data representation.
How do you access elements in a nested JSON array?
To access elements in a nested JSON array (multidimensional array), you use multiple indexes. The first index represents the position of the outer array, & the subsequent indexes represent the positions within the inner arrays. For example, to access an element in a 2D array, you would use array[i][j], where i represents the row index & j represents the column index.
Conclusion
In this article, we have learned about JSON arrays & their different types, including arrays of strings, numbers, booleans, objects, & multidimensional arrays. We explained how to create JSON arrays, access their elements using indexes, & iterate over them using loops or array methods. JSON arrays provide a flexible & efficient way to store & transmit collections of data in a structured format.
You can also check out our other blogs on Code360.