如何使用JavaScript按identifier.id分组重组JSON结构?
identifier.id in JavaScript? I need to restructure JSON data using JavaScript. The original JSON has an array called ids, where each element contains identifier.id, id, and address fields. I want to generate a new JSON structure with an array named identifiers, grouped by the same identifier.id value. Each grouped element should include the identifier.id field and a nested registrations array to store the corresponding id and address fields.
Original JSON data:
{ "ids": [ { "id": "27675548", "identifier.id": "156473847", "address": "Rua Brasilia, 23232, Sao Benedito - BH" }, { "id": "27675569", "identifier.id": "156473847", "address": "Rua Brasilia, 11, Sao Benedito - BH" } ] }
Expected JSON structure:
{ "identifiers":[ { "identifier.id":"156473847", "registrations":[ { "id": "27675548", "address": "Rua Brasilia, 23232, Sao Benedito - BH" }, { "id": "27675569", "address": "Rua Brasilia, 11, Sao Benedito - BH" } ] } ] }
As you can see, the two elements in the original JSON have the same identifier.id value, and the target JSON groups them into the same element, storing the corresponding id and address fields in a sub-array. Can this be achieved with JavaScript?
Solution
Absolutely! You can use JavaScript's array methods like reduce() to group the data by identifier.id, then convert the grouped object into the desired array structure. Here's a step-by-step implementation:
// Original data (assuming it's part of an object) const originalData = { "ids": [ { "id": "27675548", "identifier.id": "156473847", "address": "Rua Brasilia, 23232, Sao Benedito - BH" }, { "id": "27675569", "identifier.id": "156473847", "address": "Rua Brasilia, 11, Sao Benedito - BH" } ] }; // Group the data by identifier.id const grouped = originalData.ids.reduce((acc, item) => { const key = item["identifier.id"]; // If the group doesn't exist yet, create it if (!acc[key]) { acc[key] = { "identifier.id": key, registrations: [] }; } // Push the registration details (id and address) into the group acc[key].registrations.push({ id: item.id, address: item.address }); return acc; }, {}); // Convert the grouped object to the final identifiers array const result = { identifiers: Object.values(grouped) }; console.log(JSON.stringify(result, null, 2));
Breakdown of the solution:
reduce()method: We use this to loop through theidsarray and build a temporary object where each key is a uniqueidentifier.idvalue. This helps us group related items together.- Initialize groups: For each item, if there's no existing group for its
identifier.id, we create a new group with theidentifier.idand an emptyregistrationsarray. - Populate registration data: We take the
idandaddressfrom each original item and push them into the corresponding group'sregistrationsarray. - Convert to final array: Finally, we use
Object.values()to turn our temporary grouped object into theidentifiersarray that matches your desired structure.
This approach works seamlessly even if you have multiple different identifier.id values in your original data—it will automatically group all related entries correctly.
内容的提问来源于stack exchange,提问作者Taniguchi.sales

