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.

ネストされたオブジェクトに要素を追加する

Posted at

オブジェクト操作の初心者です。
JavaScriptの配列の行数に応じて、ネスト構造になっているオブジェクトにプロパティを追加していくスクリプトを作成したいのですが、方法が分かっておりません。

最終的に作成したいオブジェクトのイメージ

//配列arrの要素が2つであれば2つ分requestBodyのlines内のプロパティが追加される

var arr = ["明細1","明細2"]

var requestBody= {
"subject": "テスト",
"lines": [ 
{
"description": "明細1"
},
{
"description": "明細2"
}
]
}

//配列arrの要素が3つであれば3つ分requestBodyのlines内のプロパティが追加される

var arr = ["明細1","明細2","明細3"]

var requestBody= {
"subject": "テスト",
"lines": [ 
{
"description": "明細1"
},
{
"description": "明細2"
},
{
"description": "明細3"
}
]
}

このように配列内の要素数に応じて ,{"description": "明細○○"} を増やしていきたいです。
参考になるメソッドがあればご教授いただけますと幸いです。

0
0
2

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?