用javascript替换一些对象和数组?

时间:2017-12-22 11:23:52

标签: javascript arrays json object reduce

我使用nodejs作为服务器端,我得到了一些json对象

这是我的带有对象的json数组

[
    {
        "id": 20,
        "gsm": "123456789",
        "firstName": "Mohamed",
        "lastName": "Sameer",
        "contactgroups": [
            {
                "contactId": 20,
                "groupId": 14,
                "group": {
                    "groupname": "Angular"
                }
            }
        ]
    },
    {
        "id": 21,
        "gsm": "987654321",
        "firstName": "Ganesh",
        "lastName": "Pandiyan",
        "contactgroups": [
            {

                "contactId": 21,
                "groupId": 14,
                "group": {
                    "groupname": "Angular"
                }
            },
            {
                "contactId": 21,
                "groupId": 15,
                "group": {
                    "groupname": "React"
                }
            }
        ]
    }
]

我想要这样的最终输出(请参阅我的groupname键):

[
    {
        "id": 20,
        "gsm": "123456789",
        "firstName": "Mohamed",
        "lastName": "Sameer",
        "contactgroups": [
            {
                "contactId": 20,
                "groupId": 14,
                "groupname": "Angular",
                "group": {}
            }
        ]
    },
    {
        "id": 21,
        "gsm": "987654321",
        "firstName": "Ganesh",
        "lastName": "Pandiyan",
        "contactgroups": [
            {

                "contactId": 21,
                "groupId": 14,
                "groupname": "Angular",
                "group": {}
            },
            {
                "contactId": 21,
                "groupId": 15,
                "groupname": "React",
                "group": {}
            }
        ]
    }
]

有可能吗?要做,哪个是最佳地图或减少或lodash?任何方法?

我想更改groupname的位置并从组对象中删除groupname。

1 个答案:

答案 0 :(得分:0)

您可以使用array#map。这将返回一个带有修改键和键的新数组。值



var orgArray = [{
    "id": 20,
    "gsm": "123456789",
    "firstName": "Mohamed",
    "lastName": "Sameer",
    "contactgroups": [{
      "contactId": 20,
      "groupId": 14,
      "group": {
        "groupname": "Angular"
      }
    }]
  },
  {
    "id": 21,
    "gsm": "987654321",
    "firstName": "Ganesh",
    "lastName": "Pandiyan",
    "contactgroups": [{
        "contactId": 21,
        "groupId": 14,
        "group": {
          "groupname": "Angular"
        }
      },
      {
        "contactId": 21,
        "groupId": 15,
        "group": {
          "groupname": "React"
        }
      }
    ]
  }
]
var newArray = orgArray.map(function(item) {
  return {
    "id": item.id,
    "gsm": item.gsm,
    "firstName": item.firstName,
    "lastName": item.lastName,
     // an IIFE which will create the new contactgroups
    "contactgroups": (function() {
      return item.contactgroups.map(function(item2, index) {
        return {
          "contactId": item2.contactId,
          "groupId": item2.groupId,
          "groupname": item2.group.groupname,
          "group": {}
        }

      })

    }())
  }
})
console.log(newArray);