JSON group object in JavaScript


In the given problem statement we are asked to group the JSON object with the help of javascript functionalities. This problem can be solved with the help of a simple algorithm in javascript.

What is JSON ?

JSON (Javascript Object Notation) is lightweight data which can be transferred between two devices. This data can be read and written by humans. JSON objects are represented in the form of key−value pairs. Keys are strings to define values. In JSON each entry is separated by a semicolon. For example − {“Car” : “Audi”}, in this example Car is a key and Audi is the value for it.

const jsonData = [
  { team: 'TeamA', code: 20 },
  { team: 'TeamB', code: 30 },
  { team: 'TeamC', code: 40 },
  { team: 'TeamD', code: 50 },
]

JSON is a text format, which is a completely independent language and it uses a simple and easily understandable syntax to show the data as a key and value pair. It is also known as arrays of values. A JSON object is enclosed by curly braces {}. The keys must be a string value and values can be any valid data type like string, number, boolean, null, array or can be a nested JSON object as well. We can also say that a value in a JSON object can be another JSON object or array.

Operations performed on JSON object

We can perform several operations on JSON object, including:

  • We can access a value by using the dot notation or bracket notation. For example obj.key or obj["key"].

  • We can modify the value as well in JSON objects with the help of dot or bracket notation. For example, obj.key = newValue or obj["key"] = newValue.

  • Third operation is Adding a key−value pair. We can perform this operation with the help of dot or bracket notation. Example: obj.newKey = newValue or obj["newKey"] = newValue.

  • We can remove a key value as well with dot or bracket notation. For example, delete obj.key.

  • Next is converting to a string. We can convert a JSON object into a string object with the help of JSON.stringify() method.

  • Vice versa to the fifth number. we can convert a string value in a JSON object with the help of JSON.parse().

  • To iterate through the key−value pairs we can use a for loop to iterate through the keys in a JSON object and access their respective values.

  • We can also make a shallow copy of a JSON object by using the Object.assign() method.

Algorithm − Accessing with index value

Step 1: At the beginning of the algorithm, create a JAvascript object and give it a name and members property. And in the members property add an array of objects with 'name' and 'age' properties.

Step 2: Now we will use a dot notation or bracket notation to access the properties of the group object and its members.

Example

// define a json object
const jsonGroup = {
    "name": "Team A",
    "members": [
        {"name": "Alex", "age": 22},
        {"name": "Mack", "age": 25},
        {"name": "Chaplin", "age": 26},
        {"name": "David", "age": 38}
    ]
};

//console the output as required
console.log(jsonGroup.name);
console.log(jsonGroup.members[0].name);
console.log(jsonGroup.members[1].age);

Output

Team A
Alex
25

Algorithm − Manipulates a JSON group object

Step 1: First we will create an object named 'group' with the following properties called name and members. And members also have two properties named 'name' and 'age'.

Step 2: Now print the group name using console.log to show the initial JSON object.

Step 3: Forwarding the second step, Now create a loop through each member in the members object and print name and age using it.

Step 4: In this step, add a new member with the help of dot notation and push method.

Step 5: After the step 4, now sort the members property by age using the sort() method of javascript.

Step 6: In the last print the name and age of all the member key in a sorted array using a for loop.

Example

// Create a group object
let group = {
  name: "Team A",
  members: [
    {name: "Alice", age: 25},
    {name: "Bob", age: 30},
    {name: "Charlie", age: 27},
    {name: "Dave", age: 35}
  ]
};

// Print the group name
console.log(`Group Name: ${group.name}`);

// Print the name and age of each member
for (let member of group.members) {
  console.log(`${member.name} (${member.age})`);
}

// Add a new member to the group
group.members.push({name: "Eva", age: 28});

// Sort the members by age
group.members.sort((a, b) => a.age - b.age);

// Print the name and age of each member again
console.log("Sorted Members:");
for (let member of group.members) {
  console.log(`${member.name} (${member.age})`);

Output

Group Name: Team A
Alice (25)
Bob (30)
Charlie (27)
Dave (35)
Sorted Members:
Alice (25)
Charlie (27)
Eva (28)
Bob (30)
Dave (35)

Time Complexity

The average time complexity of accessing a Javascript object's property is O(1), and if the object has numerous properties, the worst case is O(n). The number of group members and the individual member characteristics determine the space complexity for a JSON object.

Conclusion

Javascript can easily present a collection of related data using JSON group objects. We can structure the data's organization and access by combining properties and arrays. It's crucial to take the time and space complexity of accessing the data into account when working with large JSON documents.

Updated on: 23-Aug-2023

341 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements