1

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" ]
]
Vish
  • 383
  • 2
  • 8
  • 25

1 Answers1

0

I made some changes in your code since I think some tests are useless. See here for the complete code.

I first changed this :

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(","));
}

to this :

self.filter_searchterms = new Array();
var myElements = searchterms.split(",");
for (var i = 0; i < myElements.length; i++) {
    self.filter_searchterms.push(myElements[i]); //pushing each elements inside the list   
}

You wanted to always refill a new array, so I simplified the code removing tests. Then, instead of pushing an array into filter_searchterms, I pushed each elements of the result of the splitting.

Then I changed this :

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);
     }   
}

to this :

if ($.inArray(e, self.f_searchterms) === -1)
    self.f_searchterms.push(e);

because you were doing exactly the same bunch of code into each if statement, so again these tests are irrelevant.

Serge K.
  • 5,303
  • 1
  • 20
  • 27
  • Thank you for the reply Nathan but I wanted to ask you a question. – Vish Mar 01 '14 at 16:16
  • @Vish I am listening to you :) – Serge K. Mar 01 '14 at 17:04
  • about the declaration of the array self.filter_searchterms = new Array(); As compared to self.filter_searchterms = []; is there are difference between the implicit array and the array constructor? – Vish Mar 01 '14 at 23:34
  • @Vish No. I don't think so. It is just coding style – Serge K. Mar 02 '14 at 07:40
  • 1
    @Vish You can take a look [here](http://stackoverflow.com/questions/7375120/why-is-arr-faster-than-arr-new-array). This can lead to optimization. – Serge K. Mar 02 '14 at 07:45