Suppose I have the following two strings:
var value = "1-000-111";
var mask = " - -";
I want to subtract the mask
from the value
. In other words I want something like this:
var output = subtract(value, mask);
// output should be 1000111
What is the best way to implement subtract()
? I have written this, but that doesn't seem elegant to me.
function subtract(value, mask) {
while (mask.indexOf('-') >= 0) {
var idx = mask.indexOf('-');
value = value.substr(0, idx) + value.substr(idx + 1);
mask = mask.substr(0, idx) + mask.substr(idx + 1);
}
return value;
}
Does JavaScript have something built-in to acplish this? Note that, the masking characters are not limited to -
(dash), but can be other characters as well, like +
. But in a given case, the masking character can only be either -
or +
, which can therefore be sent to the subtract()
function, which makes handling different masking character trivial. Also, the masking characters will be in arbitrary positions.
Any language-agnostic answers are also wele.
Suppose I have the following two strings:
var value = "1-000-111";
var mask = " - -";
I want to subtract the mask
from the value
. In other words I want something like this:
var output = subtract(value, mask);
// output should be 1000111
What is the best way to implement subtract()
? I have written this, but that doesn't seem elegant to me.
function subtract(value, mask) {
while (mask.indexOf('-') >= 0) {
var idx = mask.indexOf('-');
value = value.substr(0, idx) + value.substr(idx + 1);
mask = mask.substr(0, idx) + mask.substr(idx + 1);
}
return value;
}
Does JavaScript have something built-in to acplish this? Note that, the masking characters are not limited to -
(dash), but can be other characters as well, like +
. But in a given case, the masking character can only be either -
or +
, which can therefore be sent to the subtract()
function, which makes handling different masking character trivial. Also, the masking characters will be in arbitrary positions.
Any language-agnostic answers are also wele.
Share Improve this question edited Mar 27, 2018 at 4:00 Sнаđошƒаӽ asked May 8, 2017 at 12:41 SнаđошƒаӽSнаđошƒаӽ 17.6k13 gold badges77 silver badges93 bronze badges 6- could the mask have other characters as hyphens to eliminate? – Nina Scholz Commented May 8, 2017 at 12:47
-
Your solution assumes all masks will use
-
is that the case? – epascarello Commented May 8, 2017 at 12:48 - 1 No, mask may contain other characters as well. Also, masking characters may be in arbitrary positions. – Sнаđошƒаӽ Commented May 8, 2017 at 12:56
- Does the position of the characters you want to remove matter? Does the number of characters to remove differ? – Lennholm Commented May 8, 2017 at 12:59
-
I'm assuming
value.replace(/-/g '');
doesn't meet your requirements? – Lennholm Commented May 8, 2017 at 13:03
3 Answers
Reset to default 5You could split the value string and filter the characters which are unequal to the character at the same position of the mask string.
Then join the array for a new string.
function subtract(value, mask) {
return value.split('').filter(function (a, i) {
return a !== mask[i];
}).join('');
}
console.log(subtract("1-000-111", " - -"));
console.log(subtract("foo1-000-111", "foo - -"));
Just iterate through value and check if the corresponding value in mask
is not -
var value = '1-000-111';
var mask = ' - -';
var result = '';
for (var i = 0; i < value.length; i += 1) {
if (mask[i] !== '-') {
result += value[i];
}
}
console.log(result);
Mask can be any characters if you iterate through each character of value and pare to same position in mask:
Example using Array#reduce() on split() value
var value = "1-000-111";
var mask = " - -";
var unmasked = value.split('').reduce((a,c,i) => (mask[i] === c) ? a : a+c);
console.log(unmasked )