This repository holds a proposed solution to extract the correct paths that matchs a specific pattern for a given object or array of objects.
-
Preprocess pattern: add . between each [][] and each [character][] This is done in order to consider each of the above separate keys/indexes ( keys/indexes are separated by . )
-
Extract keys/indexes from pattern (split .) and store them in patterns array
-
If patterns array is empty move to step 7
-
If head is an index ([*]) append all possible indexes ([0], [1], ..., [i], ...) and return to step 3 with patterns = patterns.tail (remove the first element) and token = token[i]
-
else If head is a key and the key is defined in the token object, append key to the current local results and return to step 3 with patterns.tail (remove the first element) and token = token[key]
-
else if head is key and key is not defined in the token object, remove the leading path from local results
-
append local results to global result
(3,4,5,6) are steps of a recursive function
#unit tests
npm run test