前提
Javascriptでcategoriesという配列の中にotherCategoriesという配列を追加したいのですが、うまくいきません。※otherCategoriesの中のオブジェクトの数は現在2つですが、こちらは変動します。
実現したいこと
JavaScript
1let categories = [ 2 { 3 _id: '/one', 4 children: [ [Object], [Object], [Object], [Object] ], 5 name: 'one' 6 }, 7 { 8 _id: '/two', 9 children: [ [Object], [Object], [Object], [Object] ], 10 name: 'two' 11 }, 12] 13 14let otherCategories = [ 15 { 16 _id: '/three', 17 children: [ [Object], [Object], [Object], [Object] ], 18 name: 'three' 19 }, 20 { 21 _id: '/four', 22 children: [ [Object], [Object], [Object], [Object] ], 23 name: 'four' 24 }, 25], 26 27//上記の配列を一つに↓のようにしたい 28 29categories = [ 30 { 31 _id: '/one', 32 children: [ [Object], [Object], [Object], [Object] ], 33 name: 'one' 34 }, 35 { 36 _id: '/two', 37 children: [ [Object], [Object], [Object], [Object] ], 38 name: 'two' 39 }, 40 { 41 _id: '/three', 42 children: [ [Object], [Object], [Object], [Object] ], 43 name: 'three' 44 }, 45 { 46 _id: '/four', 47 children: [ [Object], [Object], [Object], [Object] ], 48 name: 'four' 49 }, 50]
発生している問題・エラーメッセージ
下記のpushメソッドを使ってみたが、想定していた配列構造になりません。
JavaScript
1categories.push(otherCategories);
JavaScript
1let categories = [ 2 { 3 _id: '/one', 4 children: [ [Object], [Object], [Object], [Object] ], 5 name: 'one' 6 }, 7 { 8 _id: '/two', 9 children: [ [Object], [Object], [Object], [Object] ], 10 name: 'two' 11 }, 12 [ 13 { 14 _id: '/three', 15 children: [ [Object], [Object], [Object], [Object] ], 16 name: 'three' 17 }, 18 { 19 _id: '/four', 20 children: [ [Object], [Object], [Object], [Object] ], 21 name: 'four' 22 }, 23 ] 24]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/08 10:55