我正在尝试连接(带有uniques值(两个数组,如果第二个数组有时是字符串。
也许它有一个错误,但这是我的三次尝试:
let a = 'abcdefg'
// First try
[...new Set([...[], ...(typeof(a) == 'string'? [a]: a))]
// Second try
[...new Set([...[], [(typeof(a) == 'string'? ...[a]: ...a)]]
// Third try
[...new Set([...[], (typeof(a) == 'string'? ...[a]: ...a)]
而不是
[...new Set([...[], ...(typeof a === 'string' ? [a] : a))]
拿着,看着圆形、方形、圆形和吱吱作响的括号。
[...new Set([...[], ...(typeof a === 'string' ? [a] : a)])]
// ^
let a = 'abcdefg'
console.log([...new Set([...[], ...(typeof a === 'string' ? [a] : a)])]);
您可以使用Array.concat()
,而不是使用排列,因为它以相同的方式处理组合数组和值:
const a = 'abcdefg'
console.log([...new Set([].concat([], a))])
console.log([...new Set([].concat([], [a]))])
如果我理解正确,如果a
参数是字符串,而不是集合,那么搜索唯一值和是否需要Set是没有意义的。然后你可以短路为typeof a === 'string' ? [a] : [...new Set(a)]
let a = 'abcdefg'
const createArr = a => typeof a === 'string' ? [a] : [...new Set(a)];
console.log(createArr(a));
console.log(createArr([a,a,'aa']));