Joe Torraca
Joe Torraca

Reputation: 2018

Javascript filter common words out of a string

I am trying to filter any common words out of a string. I found this code on the internet, and it looks like it will work perfect but how do I modify it to not return a string with ','?


Current code:

function getUncommon(cquerySearch, filterCommonWords) {
var wordArr = sentence.match(/\w+/g),
    commonObj = {},
    uncommonArr = [],
    word, i;

common = common.split(',');
for ( i = 0; i < common.length; i++ ) {
    commonObj[ common[i].trim() ] = true;
}

for ( i = 0; i < wordArr.length; i++ ) {
    word = wordArr[i].trim().toLowerCase();
    if ( !commonObj[word] ) {
        uncommonArr.push(word);
    }
}
return uncommonArr;
}

This returns an array like uncommonArr = 'Return, String, Would, Go, Here'. Thanks for any help!

Upvotes: 0

Views: 392

Answers (1)

Ry-
Ry-

Reputation: 225125

The default behaviour of Array.toString is to join with a ,. Just specify a custom join string:

uncommonArr.join(' '); // Join with a space, for example

Upvotes: 4

Related Questions