私のAPIがそうのような結果を返すことになります。JSON APIレスポンスを受け取ったら、ReactのJSONオブジェクトを作成する方法は?
[
{
"id":2,
"title":"Something",
},
{
"id":231,
"title":"Another",
},
...
]
そして、私のアプリを反応させるので、私はそうのようなJSONオブジェクトを構築する必要があります。
const fields = [
{
name: 'Something',
type: 'select',
options: [
{},
{ label: '1', value: '1' },
{ label: '2', value: '2' },
],
},
{
name: 'Another',
type: 'select',
options: [
{},
{ label: '1', value: '1' },
{ label: '2', value: '2' },
],
},
...
]
オプションはに依存しない(一定に保たれますサーバーが返すもの)。
var origArray = [ {id:1, title: "xxx"},
{id:2, title: "eee"},
{id:3, title: "rrr"},
{id:4, title: "ffff"}];
const fieldoptions = {
type: 'select',
options: [
{},
{ label: '1', value: '1' },
{ label: '2', value: '2' },
]
};
const fields = origArray.map(item => ({
name: item.title,
fieldoptions
}));
console.log('fields');
console.log(fields)
問題UPDATED私は動的APIに基づいて、このJSONオブジェクトを構築することができますどのように
が結果
おかげ
はには存在しないはずfieldoptions
の親が存在しています出力。
(4)[オブジェクト、オブジェクト、オブジェクト、オブジェクト] : オブジェクト fieldoptions : オブジェクト 名 : "実行"
'Array.prototype.map()' – RomanPerekhrest
あなたは何を試してみましたか?おそらくforループだけを繰り返して、フィールド[idx] .name = result [idx] .titleを作成するだけです。これはかなり些細なようです、あなたが提供できるより多くのコンテキストがありますか? – OneNeptune
@RomanPerekhrestありがとうございました!私が受け入れることができる例に答えてもらえますか? thx – AnApprentice