sudhansu bhattarai
sudhansu bhattarai

Reputation: 55

How to append data in array in formdata?

I'm Trying to send tag's ID in a array as tags will be multiple for a single blog.

const tagsList = data.tags.map((item: {
  value: number;
  label: string;
}) => {
  return {
    label: item.label, value: item.value
  }

here is the structure of data.tags enter image description here

}

)
details.append('tags', tagsList)

I'm Trying in this way but i'm unable to do so enter image description here

Can anyone help me sending the ids of tags in an array? Thanks in advance

Upvotes: 0

Views: 75

Answers (1)

Nizar
Nizar

Reputation: 722

Here's a the code:

  1. There's an array of objects that include tags - a label and value for each tag. (Yours is called data.tags).

  2. We declare a const tagsList, and use the map method on tags to return only the value in each tag.

Your use of map was inaccurate, you don't really have to pass the structure of each tag into the map method. Feel free to check out how map works here.

Let me know if the code works for you :)

  const tags = [{value:8,label:'2'},{value:10,label:'2'}]

  const tagsList = tags.map((item) => {
    return item.value
  })

  console.log(tagsList)

Upvotes: 1

Related Questions