javascript - Filter strings in Array based on content -


i running issue, have similar array of strings in js:

var myarray = ["bedroomone", "bedroomonetwo", "bathroom"]; 

and retrieve elements in array contains keyword 'bedroom'. how can achieve such result ?

i tried in different ways without getting desired result. how should proceed ?

string.prototype.indexof:

var pattern = 'bedroom',     filtered = myarray.filter(function (str) { return str.indexof(pattern) === -1; }); 

regexp:

var pattern = /bedroom/,     filtered = myarray.filter(function (str) { return pattern.test(str); }); 

string.prototype.includes (only in moderm browsers):

var pattern = 'bedroom',     filtered = myarray.filter(function (str) { return str.includes(pattern); }); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

ios - MKMapView fails to load tiles with HTTP 410 error -

c# - How to utilize EF and LINQ to add filters and specify tables, columns, filters and order by dynamically -