This is my function which is part of an object and it accepts search terms as arg from a search field which is converted to string using toString()
method. The search can consist of comma separated keywords e.g. "cloud, data, security"
$('form').on('submit', function () {
var searchVal = [$("#cc-search--field").val().trim().toLowerCase()];
$("#cc-search--field").val('');
// vo.sTerms.push(searchVal.toString());
vo.addspinner();
vo.f_constructURL(searchVal.toString());
});
f_constructURL: function (searchterms) {
var self = this,
rank = self.f_rank,
url = self.f_url;
if (self.filter_searchterms.length == 0) {
self.filter_searchterms.push(searchterms.split(","));
} else if (self.filter_searchterms.length > 0) {
// reset the array
self.filter_searchterms = [];
self.filter_searchterms.push(searchterms.split(","));
};
$.grep(self.filter_searchterms, function (e, i) {
if (self.f_searchterms.length == 0) {
if ($.inArray(e, self.f_searchterms) === -1) self.f_searchterms.push(e);
} else if (self.f_searchterms.length > 0) {
if ($.inArray(e, self.f_searchterms) === -1) self.f_searchterms.push(e);
}
});
}
this works fine in the first run, but when I search for the same term again paired with a different keyword e.g "security, javascript" the result f_searchterms
has
[
[ "cloud", "data", "security" ],
[ "security", " javascript" ]
]
On the consequent runs I want to eliminate duplicate keywords and only add unique keywords to the f_searchterms
and the result should look like a single array with #n
elements.
[
[ "cloud", "data", "security", "javascript" ]
]