判空
const arr = []; if (arr.length === 0) { console.log('数组为空'); } else { console.log('数组不为空'); }const arr = []; if (Array.isArray(arr) && arr.length === 0) { console.log('数组为空'); } else { console.log('数组不为空'); }const arr = []; const isEmpty = arr.every(item =>!item); if (isEmpty) { console.log('数组为空'); } else { console.log('数组不为空'); }const arr = []; if (Object.keys(arr).length === 0) { console.log('数组为空'); } else { console.log('数组不为空'); }
Last updated