Search an item in nested array and return the object if found otherwise return the first available object

  angular, javascript

Data

let data = [
  {
    "brands": []
  },
  {
    "brands": []
  },
  {
    "brands": [
      {
        "name": "brand1"
      },
      {
        "name": "brand2"
      },
      {
        "name": "brand3"
      }
    ]
  },
  {
    "brands": [
      {
        "name": "brand4"
      },
      {
        "name": "brand5"
      }
    ]
  }
]

if I find brand4 its should return
{ "name": "brand4"}

if I find brandZ (if not available in DATA) then it should return the first available brand
{"name": "brand1"}

Is there a way in JavaScript?

Source: Ask Javascript Questions

LEAVE A COMMENT