Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Adding a unique id for each entry in JSON object in JavaScript
In this problem statement, our task is to add a unique id for every object entry in a JSON object with the help of JavaScript. We will use a loop and a variable to store the id for each object in the JSON object.
Understanding the Problem Statement
The problem statement is to write a function in JavaScript that adds a unique id to every item in the given JSON object. For example, if we have:
let obj = {
"entry1": {empName: "A", age: 25},
"entry2": {empName: "B", age: 30}
};
After adding unique ids, the updated object will be:
let obj = {
"entry1": {id: 0, empName: "A", age: 25},
"entry2": {id: 1, empName: "B", age: 30}
};
Method 1: Using for...in Loop
This approach iterates through each key in the object and adds an incremental id property.
const jsonData = {
"entry1": {
"empName": "Janvi",
"age": 30
},
"entry2": {
"empName": "Rani",
"age": 25
},
"entry3": {
"empName": "Babita",
"age": 40
}
};
// Function to add unique ID to JSON object
function addUniqueId(jsonData) {
let index = 0;
for (let key in jsonData) {
jsonData[key].id = index++;
}
return jsonData;
}
const resultObject = addUniqueId(jsonData);
console.log(resultObject);
{
entry1: { empName: 'Janvi', age: 30, id: 0 },
entry2: { empName: 'Rani', age: 25, id: 1 },
entry3: { empName: 'Babita', age: 40, id: 2 }
}
Method 2: Using Object.keys() with forEach
This method uses Object.keys() to get all keys and then iterates using forEach to add unique ids.
const jsonData2 = {
"user1": { "name": "Alice", "role": "admin" },
"user2": { "name": "Bob", "role": "user" },
"user3": { "name": "Charlie", "role": "moderator" }
};
function addUniqueIdWithKeys(data) {
const keys = Object.keys(data);
keys.forEach((key, index) => {
data[key].id = index + 1; // Starting from 1
});
return data;
}
const result2 = addUniqueIdWithKeys(jsonData2);
console.log(result2);
{
user1: { name: 'Alice', role: 'admin', id: 1 },
user2: { name: 'Bob', role: 'user', id: 2 },
user3: { name: 'Charlie', role: 'moderator', id: 3 }
}
Method 3: Using UUID for Unique IDs
For truly unique identifiers, you can generate UUIDs or timestamp-based IDs:
const jsonData3 = {
"product1": { "name": "Laptop", "price": 999 },
"product2": { "name": "Mouse", "price": 25 }
};
function addUUIDLikeId(data) {
for (let key in data) {
// Generate a simple unique ID using timestamp and random number
data[key].id = Date.now().toString(36) + Math.random().toString(36).substr(2);
}
return data;
}
const result3 = addUUIDLikeId(jsonData3);
console.log(result3);
{
product1: { name: 'Laptop', price: 999, id: 'lm8j9k2n3p4q5' },
product2: { name: 'Mouse', price: 25, id: 'lm8j9k2n3r6s7' }
}
Comparison
| Method | ID Type | Use Case | Uniqueness |
|---|---|---|---|
| for...in Loop | Incremental Number | Simple sequential IDs | Within object only |
| Object.keys() + forEach | Incremental Number | When you need array methods | Within object only |
| UUID-like Generation | Alphanumeric String | Globally unique IDs | Globally unique |
Complexity Analysis
Time Complexity: O(n), where n is the number of entries in the JSON object, as we need to iterate through each entry once.
Space Complexity: O(1) if modifying in place, or O(n) if creating a new object with the IDs.
Conclusion
Adding unique IDs to JSON objects is straightforward using loops. Choose incremental IDs for simple cases or UUID-like strings for globally unique identifiers. All methods have O(n) time complexity and are efficient for most use cases.
