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.
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));
_.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.
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>
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.