JSFiddle - React, Tailwind, and code Playground

by arjunasuresh3

HTML

<div>Please open console to see it</div>

JavaScript

function findObjects(obj, targetValue, finalResults) {

  function getObject(theObject) {
    let result = null;
    if (theObject instanceof Array) {
      for (let i = 0; i < theObject.length; i++) {
        getObject(theObject[i]);
      }
    } else {
      for (let prop in theObject) {
        if (theObject[prop].indexOf && theObject[prop].indexOf(targetValue) > -1) {
          console.log('----found porop', prop, ', ', theObject[prop]);
          finalResults.push(theObject);
        }
        if (theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
          getObject(theObject[prop]);
        }
        if (theObject.hasOwnProperty(prop)) {
          console.log(prop + ': ' + theObject[prop]);

        }
      }
    }
  }

  getObject(obj);

}

var myObject = [{
  'title': 'some title',
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': {
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '2',
      },
    }, {
      'channel_id': 'xxx',
      'title': 'Some Recommends',
      'options': {
        'image': 'www.hupu.com',
        'title': 'alex',
        'id': '2',
      },
    }, {
      'channel_id': 'dsa3',
      'title': 'Some Recommends',
      'options': {
        'image': 'http://www.badasd.com',
        'title': 'Boots',
        'id': '1',
      },
    }],
  }],
}];

var finalResults = [];
var result = findObjects(myObject, 'badasd', finalResults);

console.log('=================================');

console.log('finalResults: ', finalResults);