You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript按identifier.id分组重组JSON结构?

How to restructure JSON data by grouping with 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 the ids array and build a temporary object where each key is a unique identifier.id value. 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 the identifier.id and an empty registrations array.
  • Populate registration data: We take the id and address from each original item and push them into the corresponding group's registrations array.
  • Convert to final array: Finally, we use Object.values() to turn our temporary grouped object into the identifiers array 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:39:38