带有es6的map函数中的条件语句 [英] Conditional Statement in a map function with es6

查看:164
本文介绍了带有es6的map函数中的条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在map函数中使用条件语句

I nee to use the conditional statement in a map function

我正在复制SVGpath d的每个单个值,但是我不希望数组的对象ML发生这种情况

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 中写下呢?我不希望元素LM发生相乘(类似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.

此代码段将所有数字加倍,但保持ML不变.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆