【发布时间】:2021-02-03 12:42:20
【问题描述】:
我有一个带有一些动态值的数组,它将有一个数组数组,并且根据内部数组长度需要显示 OR 文本。
示例:
JSON 数据:
const renderArr = [
{
label: "Abc",
value: []
},
{
label: "XyZ",
value: ["Test", "Exam"]
},
{
label: "Dex",
value: []
},
{
label: "Mno",
value: ["Momo", "Pizza"]
},
{
label: "Pqr",
value: []
}
];
根据上面的结构,renderArr[1].value 和renderArr[3].value中有值。所以我需要在 UI 中显示这些之间的“或”文本
预期逻辑:
I want to show OR condition based on below condition
let currentValue = renderArr[index].value.length > 0 then
If nextValue = renderArr[index+1].value.length > 0 then show OR condition, NOTE: it'll check till it not found the value.length > 0 for every index
以下是我的逻辑,它不能正常工作。
我的逻辑:
shouldShowNextOr = (value, rootIndex) => {
let shouldShow = false;
let f = renderArr;
if (rootIndex < 4) {
switch (rootIndex) {
case 0:
shouldShow =
f[1].value.length > 0 ||
f[2].value.length > 0 ||
f[3].value.length > 0 ||
f[4].value.length > 0
? true
: false;
break;
case 1:
shouldShow =
f[2].value.length > 0 ||
f[3].value.length > 0 ||
f[4].value.length > 0
? true
: false;
break;
case 2:
shouldShow =
f[3].value.length > 0 || f[4].value.length > 0 ? true : false;
break;
case 3:
shouldShow = f[4].value.length > 0 ? true : false;
break;
default:
shouldShow = false;
}
}
return shouldShow;
};
电流输出
预期输出
【问题讨论】:
标签: javascript reactjs ecmascript-6 logic