0

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:

enter image description here

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"
   }
]
9
  • please add valid data and what you like to get from it (as text). Commented Nov 19, 2019 at 20:58
  • 1
    What are the myLayers.items and figures.items arrays? Commented Nov 19, 2019 at 20:58
  • Editing with the console of those vars, please wait Commented Nov 19, 2019 at 21:00
  • 2
    you data does not match that wanted new structure. please add an example which data match. Commented Nov 19, 2019 at 21:31
  • 1
    please do us a favor, remove all images and add the (source) data, you have as text and the wanted result as well as text to the question. Commented Nov 19, 2019 at 21:43

3 Answers 3

1

You are pushing the wrong object into the array. Here is how you can edit the code to get the desired result.

let data = {}; //changed array to object
const layer = {}

data.myLayers = []; //added myLayers property to data
data.figures = []; //added figures property to data
myLayers.items.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.myLayers.push(layer); //changed line
    }
  })
})

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.figures.push(element); //changed line
    }
  })
})

console.log('objResult', data)
//if you want to get the JSON string
var json = JSON.stringify(data)
console.log('jsonResult', json)

Below is a simplified code snippet to show the result.

let data = {}

data.myLayers = []
data.figures = [];

data.myLayers.push( {'type':'Layer', 'id':'101'} );
data.myLayers.push( {'type':'Layer', 'id':'102'} );

data.figures.push( {'type' : 'Figure', 'id' : 201} );
data.figures.push( {'type' : 'Figure', 'id' : 202} );

console.log('objResult', data);
console.log('jsonResult', JSON.stringify(data));

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I have a problem with this. I'm pushing the same element twice on each foreachs. I got this with your implementation i.imgur.com/7Vrl1nB.png. EDIT: Fixed, I moved the const layer = {} and const element = {} inside the foreach, now I have new elements on each iteration. Thank you very much
1

Had a go at getting the output as close to what you wanted as possible, while also trying to fix your use of an object as an array - also makes it a little cleaner.

To save you scrolling, the output looks like

[
  myLayersArray: [{
    type: 'myLayers',
    fileType: 'kml',
    layerId: 'kml-f4cb4869-e448-4574-b8d0-e0538b829388',
    id: 1749
  }],
  myFiguresArray: [{
    type: 'figures',
    layerId: 'polygon-bc371cbf-8e6d-4a26-bdbd-ad1ca3d4700c',
    id: 1031
  }, {
    type: 'figures',
    layerId: 'polygon-6e0df465-af64-437e-a451-530e3553aae5',
    id: 1026
  }]
]

let 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"
   }]
};

let figures = {
  items: [{ 
      "id":1026,
      "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":"polygon-6e0df465-af64-437e-a451-530e3553aae5",
      "fileType":"kmz"
   }, { 
      "id":1031,
      "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":"polygon-bc371cbf-8e6d-4a26-bdbd-ad1ca3d4700c",
      "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":"polygon-6e0df465-af64-437e-a451-530e3553bae5",
      "fileType":"kml"
   }]
};

let visibleLayers = {
  layers : [{ 
      "id":"polygon-bc371cbf-8e6d-4a26-bdbd-ad1ca3d4700c",
      "name":"test dibujo"
   }, { 
      "id":"polygon-6e0df465-af64-437e-a451-530e3553aae5",
      "name":"asdas"
   }, { 
      "id":"kml-f4cb4869-e448-4574-b8d0-e0538b829388",
      "name":"CUADRADO POLIGONO.KML"
   }]
};


let data = [];

let myLayersArray = [];
let myFiguresArray = [];

visibleLayers.layers.forEach((visibleLayer) => {
  myLayers.items.forEach((myLayer) => {
    if (visibleLayer.id === myLayer.layerId) {
      myLayersArray.push({
        'type': 'myLayers',
        'fileType': myLayer.fileType ? myLayer.fileType : 'other',
        'layerId': visibleLayer.id,
        'id': myLayer.id
      });
    }
  }),
  figures.items.forEach((myFigure) => {
    if (visibleLayer.id === myFigure.layerId) {
      myFiguresArray.push({
        'type': 'figures',
        'layerId': visibleLayer.id,
        'id': myFigure.id
      });
    }
  })
})

data['myLayersArray'] = myLayersArray;
data['myFiguresArray'] = myFiguresArray;

// Keeps the names
console.log(data.myLayersArray);
console.log(data.myFiguresArray);

// Can still access by index
console.log(data.myFiguresArray[0]);

Comments

0
let data = []
let myLayersArray = {}
let figuresArray = {}
let count = 0
myLayers.items.forEach( (a,i) => {
  visibleLayers.layers.forEach( (b,j) => {
    if (a.layerId === b.id) {
      let layer = {}
      layer.type = 'myLayers'
      layer.filetype = a.fileType ? a.fileType : "other"
      layer.layerId = b.id
      layer.id = a.id
      myLayersArray[count] = layer
      count++
    }
  })
})

count = 0
figures.items.forEach( (a,i) => {
  visibleLayers.layers.forEach( (b,j) => {
    if (a.layerId === b.id) {
      let element = {}
      element.type = 'figure'
      element.layerId = b.id
      element.id = a.id
      figuresArray[count] = element
      count++
    }
  })
})
data.push({'myLayersArray': myLayersArray})
data.push({'figuresArray': figuresArray})

console.log('objResult', JSON.stringify(data))

So I got this (this is what I wanted):

[ 
   { 
      "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
         }
      }
   }
]

Now I want to get rid off that count++ and use of so many let

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.