Javascript 用三元算子进行数组分解

Javascript 用三元算子进行数组分解,javascript,arrays,ecmascript-6,destructuring,Javascript,Arrays,Ecmascript 6,Destructuring,我试图(使用uniques值)连接两个数组,如果第二个数组有时是字符串 也许它有一个bug,但以下是我的三个尝试: a='abcdefg' //初试 […新集合([…[],…(typeof(a)=‘string’?[a]:a))] //第二次尝试 […新设置([…[],[(类型(a)=‘字符串’?…[a]:……a)]] //第三次尝试 […新集合([…[],(typeof(a)==“string”?…[a]:…a)] 而不是 [...new Set([...[], ...(typeof a ==

我试图(使用uniques值)连接两个数组,如果第二个数组有时是字符串

也许它有一个bug,但以下是我的三个尝试:

a='abcdefg'
//初试
[…新集合([…[],…(typeof(a)=‘string’?[a]:a))]
//第二次尝试
[…新设置([…[],[(类型(a)=‘字符串’?…[a]:……a)]]
//第三次尝试
[…新集合([…[],(typeof(a)==“string”?…[a]:…a)]
而不是

[...new Set([...[], ...(typeof a === 'string' ? [a] : a))]
采取,观看圆形,方形,圆形和挤压结束括号

[...new Set([...[], ...(typeof a === 'string' ? [a] : a)])]
//                                                      ^
a='abcdefg'

console.log([…新集合([…[],…(a的类型==='string'?[a]:a)]));
您可以使用,而不是使用排列,因为它以相同的方式处理组合数组和值:

const a='abcdefg'
console.log([…新集合([].concat([],a))]))

console.log([…new Set([].concat([],[a])]))
如果我理解正确,如果
a
参数是一个字符串,而不是一个集合,则搜索唯一值和对集合的需要是没有意义的。然后你可以短路为
typeof a=='string'?[a]:[…new Set(a)]

a='abcdefg'
const createArr=a=>typeof a=='string'?[a]:[…新集合(a)];
console.log(createArr(a));

console.log(createArr([a,a,'aa']);
请添加想要的结果-以及一些带有结果的用例。
…[]
毫无意义。作为记录,我更喜欢@OriDrori的解决方案;)