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 ?
var pattern = 'bedroom', filtered = myarray.filter(function (str) { return str.indexof(pattern) === -1; });
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
Post a Comment