Conditional Statement In A Map Function With Es6
I nee to use the conditional statement in a map function I am duplicating each single value of a path d in a SVG but i do not want this happening for the objects M and L of the arr
Solution 1:
I'm not sure why your using the reverse
function also, reversing the svg path is slightly more complicated.
This code snippet doubles all the numbers, but leaves M
and L
intact.
In effect scaling up the svg path by 200%
var array = "M 175 0 L 326.55444566227675 87.50000000000001 L 326.55444566227675 262.5 L 175 350 L 23.445554337723223 262.5 L 23.44555433772325 87.49999999999999 L 175 0".split(" ");
let neWd = array.map(x => {
if (x === 'M' || x === 'L'){
return x;
}else{
return x * 2;
}
}).join(' ')
console.log(neWd);
Solution 2:
Yes, just do it:
let neWd = array.map(x => {
if (x == "M" || x == "L")
return x; // unchangedelsereturnString(parseInt(x, 10) * 2);
}).reverse().join(' ')
Solution 3:
Using Es6 syntax
let neWd = array.map((x) => (x == "M" || x == "L") ?
x : String(parseInt(x, 10) * 2)).reverse().join(' ')
Post a Comment for "Conditional Statement In A Map Function With Es6"