0

I have the following object "list":

{
  ...
  2: {id: 35, name: 'dog Sharik'},
  3: {id: 36, name: 'cat Murzik'}
  ...
}

Need to find object which consist 'cat' word in name. What is the best way to do it (does Jquery can help with it?)

prosto.vint
  • 1,403
  • 2
  • 17
  • 30

2 Answers2

1

Iterate the loop and find it.Use a for loop to iterate the obj and return when the name property of the object contains the word cat.Here obj is your original object

CODE

function findmatching(obj,word){
    for(var key in obj){
      if(obj[key]['name'].indexOf(word)!=-1){
        return obj[key]
    }
   }
  }
 var mymatchingvalue=findmatching(userinput,word)
0

You can use lodash/underscore.js
_.filter(list, function(d) { return _.includes(d.name, 'cat') })

Ranjith
  • 169
  • 1
  • 1
  • 11