有没有办法可以在每次我想使用过滤器、映射或任何其他在数组上使用的方法时跳过检查数组长度?
const newArray = array.length ? array.filter(n=>n.id=id) : [];
const otherArray = array.length ? array.map(n=>n.id=id) : [];
最佳答案
您不必检查它们。
const testArr = [];
const anotherArr = testArr.map((item) => {
// do something with item;
return transformedItem;
});
// anotherArr is [];
const anotherArr2 = testArr.filter((item) => {
// do something with item;
return boolean;
});
// anotherArr2 is still [];
关于javascript - 在给定数组上使用方法之前,如何省略检查数组长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57676891/