Created
August 10, 2019 04:26
-
-
Save cosminpopescu14/db6adc90f6e66748a143282aa1f9353f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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