Reputation: 963
This is what I've been trying, but something is wrong with the last part, can sy please tell me whats wrong, or show me an other method of doing this. Thanks in advance.
function removeSpaces(string) {
var str = string.replace(/^\s*|\s*$/g,'')
str = str.replace(/[^a-z|A-z|\r\n]/g,'');
while (str.indexOf("\r\n\r\n") >= 0) {
str = str.replace(/\r\n\r\n/g, "\r\n");
}
words = str.split("\r\n");
var i = 0;
var j = 0;
for (i = 0; i < words.length; i++) {
for (j = i; j < words.length; j++) {
if ((i != j) && (words[i] == words[j])) {
words.splice(j,1);
j--;
}
}
}
str = words.join("\r\n");
return str;
}
Upvotes: 0
Views: 1768
Reputation: 643
You could use the filter function. The filter function of an array is returning an array containing elements which pass to a certain filter.
var isLastElement = function(element, index, array){
return (index == array.lastIndexOf(element));
};
words = words.filter(isLastElement);
In this example, the filter function goes through the elements of the initial array ad add this element to the new array only if isLastElement(element, elementIndex, initArray) return true.
The isLastElement function returns true only if the element is not present a second time in the end of the array.
Upvotes: 1