通过比较两个对象的值来比较和变换数组对象

时间:2018-08-22 09:50:35

标签: javascript arrays json object group-by

我有以下物体

[{
    "ad_id": "60002985",
    "value": [{
        "name": "hi Datta",
        "Sent_to_mobile": "2345678900",
        "Sent_from_mobile": "1234567890"
      },
      {
        "name": "hello hari",
        "Sent_to_mobile": "1234567890",
        "Sent_from_mobile": "2345678900"
      },
      {
        "name": "hi Datta",
        "Sent_to_mobile": "2345678900",
        "Sent_from_mobile": "9876543210"
      },
      {
        "name": "hello ravi",
        "Sent_to_mobile": "9876543210",
        "Sent_from_mobile": "2345678900"
      },
      {
        "name": "how good you are",
        "Sent_to_mobile": "2345678900",
        "Sent_from_mobile": "1234567890"
      }
    ]
  },
  {
    "ad_id": "60002990",
    "value": [{
        "name": "Hi Datta",
        "Sent_to_mobile": "2345678900",
        "Sent_from_mobile": "3423423422"
      },
      {
        "name": "Hello ashok",
        "Sent_to_mobile": "3423423422",
        "Sent_from_mobile": "2345678900"
      }
    ]
  }
]]

每个对象列表都有一个公共值,它将来自每个对象中的“ Sent_to_mobile”或“ Sent_from_mobile”。在这里,此示例对象的通用值为'2345678900'。


 我只需要在具有“ Sent_to_mobile”和“ Sent_from_mobile”具有相同值及其答复消息的数组对象中分组值。 Sent_to_mobile应该与另一个对象Sent_from_mobile相等。

它已经像下面的对象一样变了

 [
  {
    "ad_id": "60002985",
    "0": [
      {
        "name": "hi Datta",
        "Sent_to_mobile": "2345678900",
        "Sent_from_mobile": "1234567890"
      },{
        "name": "hello hari",
        "Sent_to_mobile": "1234567890",
        "Sent_from_mobile": "2345678900"
      },
      {
        "name": "how good you are",
        "Sent_to_mobile": "2345678900",
        "Sent_from_mobile": "1234567890"
      }
    ],
    "1":[{
        "name": "hi Datta",
        "Sent_to_mobile": "2345678900",
        "Sent_from_mobile": "9876543210"
      },
      {
        "name": "hello ravi",
        "Sent_to_mobile": "9876543210",
        "Sent_from_mobile": "2345678900"
      }]
  },
  {
    "ad_id": "60002990",
    "0": [
      {
        "name": "Hi Datta",
        "Sent_to_mobile": "2345678900",
        "Sent_from_mobile": "3423423422"
      },
      {
        "name": "Hello ashok",
        "Sent_to_mobile": "3423423422",
        "Sent_from_mobile": "2345678900"
      }
    ]
  }
]

是否有有用的脚本来转换对象

0 个答案:

没有答案