how to check a string contain in an array of object

Hi I want to check if a string is contained on an array of objects. I’ve tried some methods like indexOf or data.filter, but I can’t find a better solution. My array looks like:

  0 [   {
    "date": "12/19/2014 12:00:00 AM",
    "type": "red",
     "name": "hert",
     "to": "418",
      "newitem": [
        "new": "gt",
        "level": "typeone",}]

I want to know if the string “typeone” is inside the 0[] array.

I’ve tried try :

var result = $.grep(data[0].newitem, function(e){ return e.level =="typeone"; }); 

but I didn’t get the answer.


Try this:

data[0].newitem.filter((e) => {return e.level == 'typeone'}).length

if length > 1 //string is present

Leave a Reply

Your email address will not be published. Required fields are marked *