1

I am writing a script and I would like to remove some specific elements of an array called uniqueWords. The elements I want removed are the following: "", " " and "\n". To achieve this I tried:

var removeArray = _.remove(uniqueWords, function (word) {return word === '' || word === ' ' || word === '\n'});
var removeArray2 = _.remove(uniqueWords, function (word) {return _.indexOf(['', ' ', '\n'], word) !== -1});

console.log(removeArray);
console.log(removeArray2);

I am using the js class called loadash, but I got the following results:

["", " ", "↵"]

[]

My complete code looks like:

<!DOCTYPE html>

<html>

<script src="lodash.js"></script> 

<body>
<p id="demo"></p>

<textarea cols=150 rows=15 id="texto">
"RBD|X|RBD|C|92173~GJHGWO.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGX4.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGX6.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGX8.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGXA.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGXC.NAYE" "SAMBORNSiPOSSSTHRa"
</textarea>

<script>

var splitWords = document.getElementById("texto").value.split(/[["\|~]/);
splitWords = document.getElementById("texto").value.split(/[["\|~]/)
//document.write(splitWords.toString()); 

uniqueWords = _.uniq(splitWords);
console.log(uniqueWords);
var index = uniqueWords.indexOf("");
var index2 = uniqueWords.indexOf(" ");
var index3 = uniqueWords.indexOf("\n");
console.log(index3)

var arrayLength = uniqueWords.length;


var removeArray = _.remove(uniqueWords, function (word) {return word === '' || word === ' ' || word === '\n'});
var removeArray2 = _.remove(uniqueWords, function (word) {return _.indexOf(['', ' ', '\n'], word) !== -1});

console.log(removeArray);
console.log(removeArray2);

</script>

I would like to appreciate any suggestion to achieve this.

Chris
  • 48,555
  • 16
  • 95
  • 112
neo33
  • 1,591
  • 4
  • 13
  • 33

2 Answers2

2
  1. First, put your filter list in a more scalable data format e.g. an array.
  2. Then write a function to loop through the array we want to filter with the array that contains our filter list.

Like this:

var badList = ['c', 'd'];
var myArray = ['a', 'b', 'c', 'd'];

var removeFromList = function(badlist, arrayToBeFiltered) {
    var arrayLength = arrayToBeFiltered.length;
    var badArrayLength = badlist.length;

    // let's loop through our array that we want to filter
    for (var p = 0; p < arrayLength; p++) {

        // let's loop through our "bad list"
        for (var i = 0; i < badArrayLength; i++) {
            var index = arrayToBeFiltered.indexOf(badList[i]);

            if (index > -1) {
                arrayToBeFiltered.splice(index, 1);
            }           
        }

    }

    return arrayToBeFiltered;
}

// should return ["a", "b"]
console.log(removeFromList(badList, myArray));

Fiddle

Javascript's Splice

Phillip Chan
  • 973
  • 6
  • 17
1

Using lodash _.remove():

As @elclanrs suggested you could use filter, but if you want to use lodash, you could do:

let v = _.remove(uniqueWords, function (word) {return word !== '' && word !== ' ' && word !== '\n'});

You want to return on the items in your array that are not the ones you want to remove. You were doing the reverse.

Demo

let uniqueWords = ['a', 'bravo', '', 'c', ' ', '\n', 'delta', 123];
let removeArray = _.remove(uniqueWords, function(word) {
  return word !== '' && word !== ' ' && word !== '\n'
});
console.log(removeArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>

Using filter():

You actually don't need to use lodash to get the results you want. Use the filter() method of Array.prototype instead:

let uniqueWords = ['a', 'bravo', '', 'c', ' ', '\n', 'delta', 123];
let removeWords = uniqueWords.filter(function(item) {
  return ['', ' ', '\n'].indexOf(item) < 0 ? item : null;
});
console.log(removeWords);
Chris
  • 48,555
  • 16
  • 95
  • 112