0

UPDATE: is not duplicated with question about path of objects, my question is mix objects and arrays elements with [0] or [1] or more ..

I have this code to try to get the errors, there a different 'paths' for errors; sometimes I need to change the error message accord to the path where I found the error.

Is there a way in lodash or js to ask if exist a path of object and arrays; by example:

_includes(err., 'graphQLErrors[0].state[0].key') 

here my actual code:

  try {
    errorMessage =  err.graphQLErrors[0].state[0].key; // general error message
  } catch (e) { /* ignore the error */ }

i've been seen anothers links and solutiosn, but nothing to mix array and elements mixing elements [nElement] and arrays '.'

DDave
  • 1,400
  • 3
  • 16
  • 33
  • See also [Accessing nested JavaScript objects with string key](https://stackoverflow.com/q/6491463/218196) for how to convert a single string (`'graphQLErrors[0].state[0].key'`) to an array of properties. – Felix Kling Oct 31 '17 at 16:50
  • duplicated maybe with the link on your comments, but not with 'javascript test for existence of nested object key' can you correct it ? i ll try your second option – DDave Oct 31 '17 at 16:53
  • 1
    FYI, arrays are objects. `0` is a property just like `state`. You'd use the solution of the accepted answer in the duplicate as `checkNested(err, 'graphQLErrors', 0, 'state', 0, 'key');`. – Felix Kling Oct 31 '17 at 16:58
  • thanks the sample solutions is https://lodash.com/docs#get var object = { 'a': [{ 'b': { 'c': 3 } }] }; _.get(object, 'a[0].b.c'); – DDave Oct 31 '17 at 17:17

0 Answers0