Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.