Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Created August 10, 2019 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/db6adc90f6e66748a143282aa1f9353f to your computer and use it in GitHub Desktop.
Save cosminpopescu14/db6adc90f6e66748a143282aa1f9353f to your computer and use it in GitHub Desktop.
json='[ { "Data": 2019, "IdJudet": "09,18" }, { "Data": 2019, "IdJudet": "09,18, 20, 03" }, { "Data": 2019, "IdJudet": "09" }, { "Data": 2019, "IdJudet": null } ]';
obj = JSON.parse(json)
const ids = obj
// get only the required field
.map((item) => item.IdJudet)
// filter away null values and empty strings
.filter(Boolean)
// split by comma and trim spaces
.map((IdJudet) => IdJudet.split(",").map(id => id.trim()))
// reduce arrays to your result
.reduce((result, ids) => result.concat(ids), [])
// remove duplicates
const result = ids.filter((id, i) => ids.indexOf(id) === i)
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment