0

I have the following Json

var initialdata = [
{
  "type": "Feature",
  "properties": {
    "Region": "US",
    "SubRegion": "US-1",
    "SiteID": "234"
  },
  "geometry": {
    "type": "Point"
  }
},
{
  "type": "Feature",
  "properties": {
    "Region": "US",
    "SubRegion": "US-1",
    "SiteID": "235"
  },
  "geometry": {
    "type": "Point"
  }
},
{
  "type": "Feature",
  "properties": {
    "Region": "US",
    "SubRegion": "US-2",
    "SiteID": "234"
  },
  "geometry": {
    "type": "Point"
  }
},
{
  "type": "Feature",
  "properties": {
    "Region": "UK",
    "SubRegion": "UKK",
    "SiteID": "121"
  },
  "geometry": {
    "type": "Point"
  }
},
{
  "type": "Feature",
  "properties": {
    "Region": "UK",
    "SubRegion": "UKK",
    "SiteID": "121"
  },
  "geometry": {
    "type": "Point"
  }
}
]

I have the dropdown based on the keys of initialdata.properties for example I have the dropdown like below

Region
SubRegion
SiteID

If user choose Region dropdown means, I need the following output as a list for user selection with out duplication

US
UK

How can I achieve this?

If anyone have any idea please share it with me

1 Answer 1

0

You can use a function which will reduce this to an array which you can use for your dropdown.

initialData.reduce((prev, cur) => {
     if(!prev.includes(cur.properties.Region)) {
        prev.push(cur.properties.Region)      
}
return prev
}, [])

And if instead of region you need another key in properties, you can store that value in the a const and use it in the reduce function. Ex: cur.properties[dropdownSelected]

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

2 Comments

Thank you for your reply, but its throwing some undefined includes error. can you please give me one sample based on my code?
Pls check the code above, I have edited it.

Your Answer

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