I want to create an array with 2 objects, and each of those object will have objects inside:
This is the expected result:
[
{
"myLayersArray":{
"0":{
"type":"myLayers",
"filetype":"kml",
"layerId":"kml-a00c9798-e2a5-4098-9814-0356527fc220",
"id":1749
}
}
},
{
"figuresArray":{
"0":{
"type":"figure",
"layerId":"polygon-6e0df465-af64-437e-a451-530e3553aae5",
"id":1026
},
"1":{
"type":"figure",
"layerId":"polygon-bc371cbf-8e6d-4a26-bdbd-ad1ca3d4700c",
"id":1031
}
}
}
]
Currently, I have 2 foreachs like this:
let data = []
const layer = {}
myLayers.layers.forEach( (a,i) => {
visibleLayers.layers.forEach( (b,j) => {
if (a.layerId === b.id) {
layer.type = 'myLayers'
layer.filetype = a.fileType ? a.fileType : "other"
layer.layerId = b.id
layer.id = a.id
data.push({'myLayers': layer})
}
})
})
const element = {}
figures.items.forEach( (a,i) => {
visibleLayers.layers.forEach( (b,j) => {
if (a.layerId === b.id) {
element.type = 'figure'
element.layerId = b.id
element.id = a.id
data.push({'figures': element})
}
})
})
console.log('objResult', data)
But I got this:
What should I change? I want to have just 1 figure object with all their objects inside
EDIT:
This is the data:
myLayers.items[
{
"id":1750,
"user_id":1020,
"filename":"Curico_Test_Share7.kmz",
"fullname":"1572898499.kmz",
"full_path":"https://georesearch-mastergeo.s3.amazonaws.com/1572898499.kmz",
"file_extension":"kmz",
"file_size":"6276",
"mime_type":"application/zip",
"shared":false,
"created_at":"2019-11-04 20:14:59",
"updated_at":"2019-11-04 20:14:59",
"alias":"CURICO_TEST_SHARE7.KMZ",
"information":null,
"profile_id":1,
"layerId":"kmz-0eeb9a1d-5442-477e-bca3-b7fadd6900d2",
"fileType":"kmz"
},
{
"id":1749,
"user_id":1020,
"filename":"cuadrado poligono.kml",
"fullname":"1572898478.kml",
"full_path":"https://georesearch-mastergeo.s3.amazonaws.com/1572898478.kml",
"file_extension":"kml",
"file_size":"376",
"mime_type":"text/xml",
"shared":false,
"created_at":"2019-11-04 20:14:38",
"updated_at":"2019-11-04 20:14:38",
"alias":"CUADRADO POLIGONO.KML",
"information":null,
"profile_id":1,
"layerId":"kml-f4cb4869-e448-4574-b8d0-e0538b829388",
"fileType":"kml",
"isActived":true
},
{
"id":1751,
"user_id":1020,
"filename":"test kmz.kml",
"fullname":"1572898515.kml",
"full_path":"https://georesearch-mastergeo.s3.amazonaws.com/1572898515.kml",
"file_extension":"kml",
"file_size":"420",
"mime_type":"text/xml",
"shared":false,
"created_at":"2019-11-04 20:15:15",
"updated_at":"2019-11-04 20:15:15",
"alias":"TEST KMZ.KML",
"information":null,
"profile_id":1,
"layerId":"kml-1324a441-2767-4ad9-835b-70bf4f32e730",
"fileType":"kml"
}
]
visibleLayers.layers[
{
"id":"polygon-c33a2008-edb2-415d-99e9-fab264ec1596",
"name":"test dibujo"
},
{
"id":"polygon-a373e86b-0068-444b-8a13-4743a022dfb8",
"name":"asdas"
},
{
"id":"kml-f4cb4869-e448-4574-b8d0-e0538b829388",
"name":"CUADRADO POLIGONO.KML"
}
]

myLayers.itemsandfigures.itemsarrays?