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 -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -