如何在.dmc文件中对JSON对象进行分组?

问题描述 投票:0回答:1

我有这样的JSON样本。

{
  "sending": {
    "field": [
      {
        "email": "Sample String",
        "id": 1234,
        "name": "Sample String"
      }, {
        "email": "Sample String",
        "id": 1234,
        "name": "Sample String"
      },{
        "email": "Sample String",
        "id": 1111,
        "name": "Sample String"
      }
    ]
  }
}

我想把JSON转换为按id分组,就像这样。

{
    "sending": {
        "field": [
            {
                "1234": [
                    {
                        "name": "Sample String",
                        "email": "Sample String"
                    },{
                        "name": "Sample String",
                        "email": "Sample String"
                    }

                ]
            },
            {
                "1111": [
                    {
                        "name": "Sample String",
                        "email": "Sample String"
                    }
                ]
            }
        ]
    }
}

我在.dmc文件中写了这段代码,但它没有按照我的要求工作。

   map_S_root_S_root = function(){ 
    var outputroot={};

    var a = 0;
    outputroot =  {};
    outputroot.sending =  {};
    outputroot.sending.field =  [];

    for(i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491 in inputroot.sending.field){
    outputroot.sending.field[a] =  {};
    id = inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].id;
    outputroot.sending.field[a].id =  [];

    var c =0;
    for(i in outputroot.sending.field[c].id){ 
    if (inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].id === outputroot.sending.field[c].id){
      outputroot.sending.field[c].id[c] += inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].name;
    }
    c++
    }
    outputroot.sending.field[a].id[a] =  {};
    outputroot.sending.field[a].id[a].name = inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].name;
    outputroot.sending.field[a].id[a].email = inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].email;
        a++;
        }
        return outputroot;

    };

我使用WSO2数据映射器调解器,并将代码写在集成工作室的.dmc文件(数据映射器配置文件)中,有人能帮助我吗?谢谢你的帮助

javascript json wso2 grouping datamapper
1个回答
0
投票

您可以 reduce 它。

var array={ "sending": { "field": [ { "email": "Sample String", "id": 1234, "name": "Sample String" }, { "email": "Sample String", "id": 1234, "name": "Sample String" },{ "email": "Sample String", "id": 1111, "name": "Sample String" } ] }};

array.sending.field = array.sending.field.reduce((acc,{id, ...rest})=>{
   acc[id] =acc[id] || [];
   acc[id].push(rest);
   return acc;
},{});

console.log(array);

0
投票

答案是

result = {
  "field": [{
    "id": "11",
    "name": "asma",
    "email": "asma@hotmail"
  }, {
    "id": "11",
    "name": "jone",
    "email": "jone@hotmail"
  }, {
    "id": "1234",
    "name": "jak",
    "email": "jak@hotmail"
  }]
}
results = result.field;
groups = {};
for (var i in results) {
  var groupName = results[i].id;
  if (!groups[results[i].id]) {
    groups[groupName] = [];
  }
 groups[groupName].push({"name" :results[i].name,"email":results[i].email})
}
console.log(groups);
© www.soinside.com 2019 - 2024. All rights reserved.