解构 - 错误重复声明'小时'

时间:2018-01-19 14:13:52

标签: javascript

我正在使用解构来创建开始和结束时间:

  "events": [
        {
            "id": 518,
            "global_id": "wordpress.rguc.co.uk?id=518",
            "global_id_lineage": [
                "wordpress.rguc.co.uk?id=518"
            ],
            "author": "1",
            "status": "publish",
            "date": "2017-12-14 12:11:42",
            "date_utc": "2017-12-14 12:11:42",
            "modified": "2018-01-11 10:04:49",
            "modified_utc": "2018-01-11 10:04:49",
            "url": "http://wordpress.rguc.co.uk/event/another-one/",
            "rest_url": "http://wordpress.rguc.co.uk/wp-json/tribe/events/v1/events/518",
            "title": "another one",
            "description": "",
            "excerpt": "",
            "image": false,
            "all_day": true,
            "start_date": "2018-01-31 00:00:00",
            "start_date_details": {
                "year": "2018",
                "month": "01",
                "day": "31",
                "hour": "00",
                "minutes": "00",
                "seconds": "00"
            },
            "end_date": "2018-01-31 23:59:59",
            "end_date_details": {
                "year": "2018",
                "month": "01",
                "day": "31",
                "hour": "23",
                "minutes": "59",
                "seconds": "59"
            },
            "utc_start_date": "2018-01-31 00:00:00",
            "utc_start_date_details": {
                "year": "2018",
                "month": "01",
                "day": "31",
                "hour": "00",
                "minutes": "00",
                "seconds": "00"
            },
            "utc_end_date": "2018-01-31 23:59:59",
            "utc_end_date_details": {
                "year": "2018",
                "month": "01",
                "day": "31",
                "hour": "23",
                "minutes": "59",
                "seconds": "59"
            },

有什么想法吗?

活动json:

{{1}}

2 个答案:

答案 0 :(得分:2)

解构的语法(BTW破坏是错误的术语)是不同的。您需要在冒号之前指定匹配的键和冒号后声明的变量。 也许你想要:

 const {
    year: startYear,
    month: startMonth,
    day: startDay,
    hour: startHour,
    minutes: startMinute
} = event.start_date_details;
const {
    hour: endHour,
    minutes: endMinute
} = event.end_date_details;

答案 1 :(得分:1)

您正在将event.start_date_details.startHourevent.end_date_details.endHour解构为const hour。将其更改为

const {
  start_date_details: {
    year: startYear,
    month: startMonth,
    day: startDay,
    hour: startHour,
    minutes: startMinute
  },
  end_date_details: {
    hour: endHour,
    minutes: endMinute
  }
} = event;

这样您就可以从startHour映射变量startMinuteendHourendMinuteevent.(start/end)_date_details.(hour/minutes)

请参阅MDN's Destructuring Assignment - Assigning to new variable names