我需要根据用户的偏好将多个数组添加到一起,有些数组可能看起来是空的,我如何确保我要连接的数组不是空的
这是我失败的代码
const num1 = [12,67,100]
const num2 = []
const num3 = [23,191, 58]
const num4 = [23,30]
const numbers = num1.concat(`num${Math.random() * 10}`)
const num1 = [12, 67, 100]
const num2 = []
const num3 = [23, 191, 58]
const num4 = [23, 30]
const check = `num${Math.random() * 10}`
const numbers = num1.concat(check && check)
console.log(numbers);
根据这个答案,转换为2d阵列并将其转换为单个平面阵列,似乎是的首选解决方案
const values = [ [12,67,100], [], [23,191, 58], [23,30] ]
const numbers = [].concat(...values)
console.log(`items count: ${numbers.length}`)
const randomNumbers = numbers.map(num => parseInt(num * Math.random()))
好了,很容易理解:
constant numbers;
int randomArray = `num${Math.random() * 10}`;
if (randomArray.length > 0)
{
numbers = num1.concat(randomArray);
}