0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FormDataに一次元配列をappendする

Posted at

#目的
FormDataを使ってAPIに配列をpostしたい場合にどのように送ればいいのか悩んだため、アウトプットする。

#完成

component.vue
<script>
data () {
    return {
        tag:[]
  }
}
let formData = new FormData();
if (this.tag.length > 0) {
  this.tag.forEach((text, index) => {
    formData.append('tag[' + index + ']', text);
  })
} else {
  formData.append('tag', []);
  }
</script>

##解説
1.tag[]に1つ以上値が存在するか確認する
 1-1.存在する場合、tag[]forEachで一つずつappendする!
 1−2.存在しない場合、空の配列をappend

JavaScriptはforeachではなくforEachらしいので注意が必要

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?