尝试使用for循环循环数组列表,但如果满足则条件失败。
如果有2个数组列表,说1有数据而其他没有数据
它仅循环遍历第一个数组,但不循环使用其他数组列表。
我只是检查值是否为空,未定义和0。
如果数组列表没有值,我会弹出警告消息,但是首先列出数据是否可用且它是否已经循环但是没有检查其他值是否满足。
我可以就此得到一些建议吗?请这是我的代码
areWidgetsEmpty = (currentWidgetList) => {
for (let i = 0; i < currentWidgetList.length; i++) {
const widget = currentWidgetList[i];
switch (widget.widgetType) {
case WidgetTypes.venn: {
const CurrentWidgetRigthValue = widget.rightTargetValue;
const CurrentWidgetLeftValue = widget.leftTargetValue;
const CurrentWidgetRightText = widget.rightTarget;
const CurrentWidgetLeftText = widget.leftTarget;
return !CurrentWidgetRigthValue || !CurrentWidgetLeftValue || !CurrentWidgetRightText || !CurrentWidgetLeftText;
};
default:
return false;
}
}
}
currentWidgetList
0
:
{widgetId: "ee23235b-4034-4443-98fc-47b783610311", widgetName: "widget 0", widgetType: "venn", leftTarget: "PSHGMALE", rightTarget: "PSHGMALE", …}
1
:
{widgetId: "625927bf-bacb-447d-aeb5-517940293c12", widgetName: "Widget", widgetType: "venn", leftTarget: "", rightTarget: "", …}
答案 0 :(得分:-1)
你可以试试这个。目前尚不清楚“空”究竟是什么意思,但您可以定义nonEmpty
函数并使用Array.every()
来检查数组中的所有项是否满足函数中定义的条件。
const nonEmpty = ({
widgetType,
rightTarget,
leftTarget
}) => widgetType == 'venn' ? rightTarget && leftTarget : true
const areWidgetsEmpty = items => !items.every(nonEmpty)
const widgets = [{
widgetType: "venn",
leftTarget: "PSHGMALE",
rightTarget: "PSHGMALE"
}, {
widgetType: "other",
leftTarget: "",
rightTarget: ""
}]
console.log(areWidgetsEmpty(widgets))