带有es6的map函数中的条件语句 [英] Conditional Statement in a map function with es6
问题描述
我需要在map函数中使用条件语句
I nee to use the conditional statement in a map function
我正在复制SVG
中path d
的每个单个值,但是我不希望数组的对象M
和L
发生这种情况
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 array
这是数组作为字符串的示例.
Here is an example of the array as string.
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
这是我的情况下没有条件陈述的示例
This is an example to my case without the conditional statement
let neWd = array.map(x => { return x * 2; }).reverse().join(' ')
如何在 e6 中写下呢?我不希望元素L
和M
发生相乘(类似if x ? 'M' : 'L' return
)
how can i write down this in e6 ? I don not want the multiplication happening for the elements L
and M
( something like if x ? 'M' : 'L' return
)
推荐答案
我不确定为什么还要使用reverse
函数,反转svg路径会稍微复杂些.
I'm not sure why your using the reverse
function also, reversing the svg path is slightly more complicated.
此代码段将所有数字加倍,但保持M
和L
不变.
This code snippet doubles all the numbers, but leaves M
and L
intact.
实际上将svg路径扩大了200%
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);
这篇关于带有es6的map函数中的条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!