1

I have an array of objects which are formatted like this:

{
    "gallery": [{
        "id": 606,
        "status": 1,
        "name": "00000000606.png",
        "title": "splash.png",
        "location": "",
        "caption": "",
        "type": "image/png",
        "charset": "binary",
        "tags": "",
        "width": 2732,
        "height": 2732,
        "size": 476358,
        "embed_id": null,
        "user": 1,
        "date_uploaded": "2019-09-26T05:22:31-04:00",
        "storage_adapter": "local",
        "url": "/storage/uploads/00000000606.png",
        "thumbnail_url": "/storage/uploads/thumbs/606.png",
        "old_thumbnail_url": "/storage/uploads/thumbs/00000000606-png-160-160-true.jpg",
        "html": null
    }, {
        "id": 610,
        "status": 1,
        "name": "00000000610.png",
        "title": "icon.png",
        "location": "",
        "caption": "",
        "type": "image/png",
        "charset": "binary",
        "tags": "",
        "width": 1024,
        "height": 1024,
        "size": 274477,
        "embed_id": null,
        "user": 1,
        "date_uploaded": "2019-09-26T06:43:44-04:00",
        "storage_adapter": "local",
        "url": "/storage/uploads/00000000610.png",
        "thumbnail_url": "/storage/uploads/thumbs/610.png",
        "old_thumbnail_url": "/storage/uploads/thumbs/00000000610-png-160-160-true.jpg",
        "html": null
    }]
}

What I would like to do is set the data for posting as follows:

{
    gallery: [
        {id: 606},
        {id: 610}
    ]
}

I have tried to do:

const imageId = this.selectedGallery.map(({id}) => id );

then setting the gallery array like this:

{
  gallery: [
      {id: imageId},
  ]
}

this posts the full array to id: and fails.

How would I approach this?

12
  • The correct syntax is .map(({ id }) => id) Commented Sep 26, 2019 at 13:22
  • 3
    For the desired output, it's .map(({ id }) => ({ id })) Commented Sep 26, 2019 at 13:23
  • 1
    If you want to post the answer I can then tick it to give people a view that this has been resolved. Commented Sep 26, 2019 at 13:40
  • 1
    @Apex sure, let me do this, then answer another comment Commented Sep 26, 2019 at 13:41
  • 1
    @AdritaSharma no problem, remember that this syntax is just a shortcut for quick object manipulation, but not for data manipulation Commented Sep 26, 2019 at 13:51

1 Answer 1

5

When you use that kind of one-liner, you have a specific syntax to follow :

.map(   (   {   id   }   )   =>   (   {   id   }   )   );
 _|_   _|_ _|_  _|_         _|_  _|_ _|_  _|_
  1     2   3    4           5    6   7    8

1 - the operator you are going to use

2 - the parenthesis that will be used to contain your parameters declaration. You can omit it if you have a single parameter. In TS, if you type it, you're forced to put the parenthesis anyway.

3 - The deconstruction bracket. Between those brackets, you can selectively pick properties in your object. In your case, you are picking only the ID.

4 - the properties to pick (1 or more, comma separated)

5 - The fat arrow to write a one-liner

6 - The evaluating parenthesis : this one is a bit tricky, a Stack answer wouldn't even be enough to explain it. The best to understand it is to play with it. In this case, see that parenthesis as a way of returning an object : since the function body (function() {}) and object declaration (obj = {}) use the same bracketed-syntax, the parenthesis changes it to return an object instead of a function body.

7 - the bracket for the object declaration

8 - the properties to use. When writing a single property ({ id } instead of { id: id }), it simply reduces the syntax but prevents from doing changes to that variable.

The final syntax would then be

.map(({ id }) => ({ id }))
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.