简体   繁体   English

带有 es6 的映射函数中的条件语句

[英]Conditional Statement in a map function with es6

I nee to use the conditional statement in a map function我需要在 map 函数中使用条件语句

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我在SVG复制path d每个单个值,但我不希望数组的对象ML发生这种情况

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(' ')

how can i write down this in e6 ?我如何在e6 中写下这个? I don not want the multiplication happening for the elements L and M ( something like if x ? 'M' : 'L' return )我不希望元素LM发生乘法(类似于if x ? 'M' : 'L' return

I'm not sure why your using the reverse function also, reversing the svg path is slightly more complicated.我不确定为什么你也使用reverse函数,反转 svg 路径稍微复杂一些。

This code snippet doubles all the numbers, but leaves M and L intact.此代码片段将所有数字翻倍,但保持ML不变。

In effect scaling up the svg path by 200%实际上将 svg 路径扩大了 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);

Yes, just do it:是的,就这样做:

let neWd = array.map(x => {
    if (x == "M" || x == "L")
        return x; // unchanged
    else
        return String(parseInt(x, 10) * 2);
}).reverse().join(' ')

Using Es6 syntax使用 Es6 语法

let neWd = array.map((x) => (x == "M" || x == "L") ?
x : String(parseInt(x, 10) * 2)).reverse().join(' ')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM