0

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

9
  • Please add the structure of data.tags Commented Oct 6, 2021 at 7:41
  • @Nizar I have added. Thankyou Commented Oct 6, 2021 at 7:46
  • Can you also please share what you expect tagsList to be (if the code was correct, what would tagsList look like)? I don't fully understand. Where is the id of the tag stored? In item.label or item.value? Commented Oct 6, 2021 at 7:50
  • I want to send data in tags as[8,9] which is the id of tags(multiple). And Each tags has label and value. I want to send its value Commented Oct 6, 2021 at 7:57
  • But where does the id come from? data.tags only includes label and value, but no id. Commented Oct 6, 2021 at 7:58

1 Answer 1

1

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)

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

3 Comments

I am getting tags: 8,10 in network header but throws this error "["Incorrect type. Expected pk value, received str."]" and throws 400 error.
Thats a django question I assume. Now you're going to have to ask another question on the django forum :) The goal of this question is to give you the array you need in the form you wanted
Thankyou for the help and sharing your thoughts. It solved the goal of this question.

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.