0

I'm trying to rename some objects from one array (eventFetch) and pass them into another (mapEvents). I'm trying destructuring aliases to do this, but I'm trying to rename a nested object so I don't think this is the right approach.

Here's an example of the eventFetch array:

 "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"
            }, 

And the destructure I'm trying:

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

I want to rename name.text to title rather than just renaming name

1 Answer 1

3

You could destructure it in the following way:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.