嵌套对象的结构化数组别名

时间:2019-07-19 05:40:51

标签: javascript arrays reactjs ecmascript-6

我正在尝试从一个数组(eventFetch)重命名一些对象,然后将它们传递给另一个(mapEvents)。我正在尝试通过分解别名来做到这一点,但是我正在尝试重命名嵌套对象,因此我认为这不是正确的方法。

这是eventFetch数组的示例:

 "events": [
        {
            "name": {
                "text": Event Name, 
                "html": null
            }, 
            "description": {
                "text": null, 
                "html": null
            }, 
            "id": "23412412", 
            "url": "https://www.eventbrite.com.au/e/eventname", 
            "start": {
                "timezone": "Australia/Sydney", 
                "local": "2016-05-28T19:00:00", 
                "utc": "2016-05-28T09:00:00Z"
            }, 
            "end": {
                "timezone": "Australia/Sydney", 
                "local": "2016-05-28T22:00:00", 
                "utc": "2016-05-28T12:00:00Z"
            }, 

以及我正在尝试的破坏:

const mapEvents = eventFetch.map(({ name: title, start: start, end: 
end, url }) => ({ title, start, end, url }));

我想将name.text重命名为标题,而不仅仅是重命名name

1 个答案:

答案 0 :(得分:2)

您可以通过以下方式对其进行破坏:

var eventMap = {
  "events": [{
    "name": {
      "text": "Event Name",
      "html": null
    },
    "description": {
      "text": null,
      "html": null
    },
    "id": "23412412",
    "url": "https://www.eventbrite.com.au/e/eventname",
    "start": {
      "timezone": "Australia/Sydney",
      "local": "2016-05-28T19:00:00",
      "utc": "2016-05-28T09:00:00Z"
    },
    "end": {
      "timezone": "Australia/Sydney",
      "local": "2016-05-28T22:00:00",
      "utc": "2016-05-28T12:00:00Z"
    }
  }]
};
const mapEvents = eventMap.events.map(({ name: {text:title, html}, start, end, url }) => ({ name:{title, html}, start, end, url }));
console.log(mapEvents);