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

Replace some objects and arrays using javascript?

我正在使用 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"
                }
            }
        ]
    }
]

我想要这样的最终输出(查看我的组名键):

[
    {
        "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": {}
            }
        ]
    }
]

可能吗?做,哪个是最好的 map 或 reduce 或 lodash?有什么方法吗?

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

您可以使用 array#map。这将 return 一个具有修改后的键和值的新数组

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);