显示无限数量的未定义数组,javascript
指: 我试图显示一个由无限多个对象数组创建的数组,有些数组可能未定义。 有没有更好的写作方法。 我对那个问题的解决方案看起来不专业显示无限数量的未定义数组,javascript,javascript,arrays,json,concatenation,Javascript,Arrays,Json,Concatenation,指: 我试图显示一个由无限多个对象数组创建的数组,有些数组可能未定义。 有没有更好的写作方法。 我对那个问题的解决方案看起来不专业 const theFunction = () => { let EveryArrayGoesHere = [] try { EveryArrayGoesHere = EveryArrayGoesHere.concat(array1, array2, array3 ...) if (EveryArrayGoesHe
const theFunction = () => {
let EveryArrayGoesHere = []
try {
EveryArrayGoesHere = EveryArrayGoesHere.concat(array1, array2, array3 ...)
if (EveryArrayGoesHere) {
EveryArrayGoesHere = JSON.stringify(EveryArrayGoesHere)
EveryArrayGoesHere = EveryArrayGoesHere.replace('null,', '')
EveryArrayGoesHere = EveryArrayGoesHere.replace(',null', '')
return JSON.parse(EveryArrayGoesHere);
} else {
return 'data not availible';
}
} catch (e) {
if (e) {
console.error('data error', e.message)
}
}
}
console.log('array of objects:',theFunction() )
我希望我正确理解了你的问题
var收集器=[];
风险值数据=[
[1,2,3],
未定义,
[4,5,6],
[7,8],
未定义,
[9]
];
//log('data=>',data);
data=data.filter(v=>{returnarray.isArray(v)}).forEach(a=>{collector=collector.concat(a)});
log('collector=>',collector)代码>某些数组可能未定义或某些对象?数组可能未定义,thanksmay数组可能只包含未定义的值,即[undefined,undefined]…解决方案适用于包含对象“[{”product:“apple”,“trueFalse”:true,“price”:1.5},{”product:“banana”的数组,“trueFalse”:true,“price”:1}]`此解决方案可能适用于具有类似结构的数组。`Array.isArray`完成此工作,谢谢@Reflective