0

My JSON is like this (it's valid, it's just too long so I will only paste a part of it):

const world = {
  "object": {
    "countries": {
      "type": "GeometryCollection",
      "geometries": [{
          "type": "Polygon",
          "id": 4,
          "arcs": [
            [
              297,
              298,
              299,
              300,
              301,
              302
            ]
          ]
        },
        {
          "type": "MultiPolygon",
          "id": 24,
          "arcs": [
            [
              [
                303,
                304,
                211,
                305
              ]
            ],
            [
              [
                213,
                306,
                307
              ]
            ]
          ]
        },
        {
          "type": "Polygon",
          "id": 8,
          "arcs": [
            [
              308,
              248,
              309,
              310,
              311
            ]
          ]
        }
      ]
    }
  }
}

It's basically a country list, and each country has its own ID.

Now, I was able to select all countries by world.objects.countries, but I need to select countries with specific IDs, not all of them: either with "id":24 or "id":4.

I guess I need to use a filter to filter countries with specific IDs inside of them? I'm really just a beginner and the solutions I found are filtered with jQuery, not vanilla javascript.

SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
lastnoob
  • 646
  • 12
  • 28
  • 2
    What is desired output ? in posted data countries do not have id. geometries do have ? – Code Maniac Feb 09 '19 at 12:02
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter – JGFMK Feb 09 '19 at 12:07
  • 1
    Possible duplicate of [Javascript: How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – adiga Feb 09 '19 at 12:09

1 Answers1

0

I guess each "geometry" represents a country. Then:

const world = {
objects: {
"countries":{
         "type":"GeometryCollection",
         "geometries":[
            {
               "type":"Polygon",
               "id":4,
               "arcs":[
                  [
                     297,
                     298,
                     299,
                     300,
                     301,
                     302
                  ]
               ]
            },
            {
               "type":"MultiPolygon",
               "id":24,
               "arcs":[
                  [
                     [
                        303,
                        304,
                        211,
                        305
                     ]
                  ],
                  [
                     [
                        213,
                        306,
                        307
                     ]
                  ]
               ]
            },
            {
               "type":"Polygon",
               "id":8,
               "arcs":[
                  [
                     308,
                     248,
                     309,
                     310,
                     311
                  ]
               ]
            }
      ]
}
}
}

const result = world.objects.countries.geometries.filter(g => [4, 24].includes(g.id))
console.log(result)

filter function was used.

Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89