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 1 year has passed since last update.

【JavaScript】配列の要素を1つの文字列に連結する方法

Last updated at Posted at 2022-09-02

メモです。

join()メソッドを使用すると
配列の全要素を順に連結した文字列を新たに作成して返す。

const array = ['aaa', 'bbb', 'ccc'];

array.join() // "aaa,bbb,ccc";
array.join('') // "aaabbbccc";
array.join('-') // "aaa-bbb-ccc";

array.length0だった場合、空の文字列が返される。

&で連結すると、クエリパラメータを渡す時に便利。

 // フォームからの入力などが以下だったとして
 const year = '2022';
 const month = '09';
 const date = '02';
 const page = '1';
 const limit = '30';

 const baseUrl = '/api/v1/data_export/export_csv';

  const queries =[
    'year='+ year,
    'month='+ month,
    'date='+ date,
    'page='+ page,
    'limit='+ limit
  ];
  const href = baseUrl + '?' + queries.join('&');

コメント欄にて、URLオブジェクトを生成して書く方法を教えていただきました!
ありがとうございます。

参考:
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/join`

0
0
4

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?