如果 javascript 中为空,如何跳过数组
How to skip array if empty in javascript
我从 json url 到 json_decode 创建了一个数组。我用各种嵌套数组做了一些事情。我还想用所有嵌套数组中的数据做一些事情。因此,我认为将数组再次连接成一个数组(但现在不是嵌套的)并循环遍历它会很方便。 concat 工作完美。但是当我尝试对 key/value 上的新数组进行排序时,它出错了,我知道为什么。一个或多个有时为空的数组(动态数据源)。因此数组例如是;
"various":null
这就是我为脚本创建各种变量的方式。
var data_hazards = data_all['alerts'][0]['hazards'];
var data_various = data_all['alerts'][0]['various'];
var data_standstill = data_all['alerts'][0]['standstill'];
var data_roadclosed = data_all['alerts'][0]['roadclosed'];
var data_accidents = data_all['alerts'][0]['accidents'];
var data_police = data_all['alerts'][0]['police'];
var data_construction = data_all['alerts'][0]['constructions'];
var data_jams = data_all['alerts'][0]['jams'];
//Concat the above arrays
var data_total = data_hazards.concat(data_various).concat(data_standstill).concat(data_roadclosed).concat(data_accidents).concat(data_police).concat(data_construction).concat(data_jams);
现在我想知道如何在 concat() 中跳过空数组。
有人有想法吗?
很高兴收到你的来信。
此致,
路德
您可以使用默认的空数组来连接所有数组。您不需要链接 Array.concat
,因为您可以使用更多数组作为参数。
var data_total = [].concat(
data_hazards || [],
data_various || [],
data_standstill || [],
data_roadclosed || [],
data_accidents || [],
data_police || [],
data_construction || [],
data_jams || []
);
另一种解决方案可以是包含值的数组。
var data_total = [
data_hazards, data_various, data_standstill, data_roadclosed, data_accidents, data_police, data_construction, data_jams
].reduce((r, a) => r.concat(a || []), []);
空数组或默认数组的虚假值的测试用例:
// concat empty array to an empty array
console.log([].concat([])); // []
// use default array if falsy value
console.log([].concat(null || [])); // []
// concat a value to an empty array
console.log([].concat(['foo'])); // ['foo']
您的代码中存在明显的重复模式,因此存在大量重复。我建议使用以下方法解决该问题并同时解决您的问题:
const data_total =
['hazards', 'various', 'standstill', ...]
.map(key => data_all['alerts'][0][key]) // retrieve data for each key
.filter(data => data != null) // ignore those that are null
.reduce((a, b) => a.concat(b), []); // concat them all together
我从 json url 到 json_decode 创建了一个数组。我用各种嵌套数组做了一些事情。我还想用所有嵌套数组中的数据做一些事情。因此,我认为将数组再次连接成一个数组(但现在不是嵌套的)并循环遍历它会很方便。 concat 工作完美。但是当我尝试对 key/value 上的新数组进行排序时,它出错了,我知道为什么。一个或多个有时为空的数组(动态数据源)。因此数组例如是;
"various":null
这就是我为脚本创建各种变量的方式。
var data_hazards = data_all['alerts'][0]['hazards'];
var data_various = data_all['alerts'][0]['various'];
var data_standstill = data_all['alerts'][0]['standstill'];
var data_roadclosed = data_all['alerts'][0]['roadclosed'];
var data_accidents = data_all['alerts'][0]['accidents'];
var data_police = data_all['alerts'][0]['police'];
var data_construction = data_all['alerts'][0]['constructions'];
var data_jams = data_all['alerts'][0]['jams'];
//Concat the above arrays
var data_total = data_hazards.concat(data_various).concat(data_standstill).concat(data_roadclosed).concat(data_accidents).concat(data_police).concat(data_construction).concat(data_jams);
现在我想知道如何在 concat() 中跳过空数组。
有人有想法吗?
很高兴收到你的来信。
此致,
路德
您可以使用默认的空数组来连接所有数组。您不需要链接 Array.concat
,因为您可以使用更多数组作为参数。
var data_total = [].concat(
data_hazards || [],
data_various || [],
data_standstill || [],
data_roadclosed || [],
data_accidents || [],
data_police || [],
data_construction || [],
data_jams || []
);
另一种解决方案可以是包含值的数组。
var data_total = [
data_hazards, data_various, data_standstill, data_roadclosed, data_accidents, data_police, data_construction, data_jams
].reduce((r, a) => r.concat(a || []), []);
空数组或默认数组的虚假值的测试用例:
// concat empty array to an empty array
console.log([].concat([])); // []
// use default array if falsy value
console.log([].concat(null || [])); // []
// concat a value to an empty array
console.log([].concat(['foo'])); // ['foo']
您的代码中存在明显的重复模式,因此存在大量重复。我建议使用以下方法解决该问题并同时解决您的问题:
const data_total =
['hazards', 'various', 'standstill', ...]
.map(key => data_all['alerts'][0][key]) // retrieve data for each key
.filter(data => data != null) // ignore those that are null
.reduce((a, b) => a.concat(b), []); // concat them all together