3

lodash where method is not considering nested array elements when searching. Consider the below example:

var foo = [
  {
    "id": "123",
    "elements": [
      {
        "id": "1231",
        "name": "foo1"
      },
      {
        "id": "1232",
        "name": "bar1"
      }
    ]
  },
  {
    "id": "456",
    "items": [
      {
        "id": "4561",
        "name": "foo2"
      },
      {
        "id": "4562",
        "name": "bar2"
      }
    ]
  }
]

_.where(foo, { 'id' : '123' } ) //works and returns the element
_.where(foo, { 'id' : '1231' } ) //fails.. trying to match the first element in the elements array. does not return matched element

Is there an API in lodash or other JavaScript library to perform deep search?

suman j
  • 6,710
  • 11
  • 58
  • 109
  • It cannot know the structure of your object. Using `_.where(foo, {'elements': [{ 'id' : '1231' }]} )` will work. (*it will return the top level that contains it*) – Gabriele Petrioli Dec 23 '14 at 16:20
  • above is simplified scenario of actual issue at hand. In my case "key" `elements` is not constants. It could be "elements", "items", "things" etc and it will be nested deep. 3rd, 4th , 5th level .... Only constant part is "id" key – suman j Dec 23 '14 at 16:22
  • and what do you want returned ? the top level object that contains it or the final object that has that property/value combo ? – Gabriele Petrioli Dec 23 '14 at 16:30
  • @Gaby the final object. – suman j Dec 23 '14 at 16:53
  • I closed the question. The answer there, works as-is for lodash as well. – Gabriele Petrioli Dec 23 '14 at 17:08

0 Answers0