I have the following object and lodash "queries" in Node.js (I'm simply running node in the terminal):
var obj = {
  a: [{
    b: [{
      c: "apple"
    },
    {
      d: "not apple"
    },
    {
      c: "pineapple"
    }]
  }]
};
> _.get(obj, "a[0].b[0].c")
'apple'
> _.get(obj, "a[0].b[1].c")
undefined
> _.get(obj, "a[0].b[2].c")
'pineapple'
My question is: is there a way to return an array of values where the path was found to be valid?
Example:
> _.get(obj, "a[].b[].c")
['apple', 'pineapple']
                As @Tomalak has suggested in a comment, the solution was to use JSONPath instead of Lodash.
Their github page: https://github.com/dchester/jsonpath
Example:
> var jp = require("jsonpath")
> var obj = {
   a: [{
     b: [{
       c: "apple"
     },
     {
       d: "not apple"
     },
     {
       c: "pineapple"
     }]
   }]
 };
> jp.query(obj, "$.a[*].b[*].c")
[ 'apple', 'pineapple' ]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With