地图内的 if 语句
Posted
技术标签:
【中文标题】地图内的 if 语句【英文标题】:If Statement Inside Map 【发布时间】:2022-01-19 02:54:18 【问题描述】:我目前有一个 DataGrid,它正在渲染从我的后端 mongoDB 抓取的数据。数据渲染映射到 mongoDB 文档中的对象指定的键。在每个文档中都有一组布尔值,我正在尝试检查其中任何一个是否为真,如果它们为真,它将在每行的 repairsNeeded 列中呈现一个 Y,如果不是,它将呈现一个 N。我遇到的主要问题是在哪里/如何检查这个。我尝试了一些不同的想法,但无济于事。现在,我为 document.isPowerCordDamaged(我的布尔值之一)分配了每一行的 repairsNeeded 列,这取决于它是否被选中。
代码:
function Rounding()
const [cartsRounded, setCartsRounded] = useState([]);
let navigate = useNavigate();
useEffect(() =>
userCartsRounded()
.then((response) =>
setCartsRounded(response.data);
)
.catch((err) =>
console.log(err);
);
, []);
const columns = [
field: "serialNum",
headerName: "Cart Serial Number",
width: 250,
,
field: "pcNum",
headerName: "Workstation Number",
width: 250,
,
field: "dateLastRounded",
headerName: "Last Rounded On",
width: 250,
,
field: "repairsNeeded",
headerName: "Repairs?",
width: 100,
,
field: "quarter",
headerName: "Quarter",
width: 75,
,
];
const [sortModel, setSortModel] = React.useState([
field: "dateLastRounded",
sort: "desc",
,
]);
const rows = useMemo(
() =>
cartsRounded.map((row, index) => (
...row,
id: index,
serialNum: row.cartSerialNumber,
pcNum: row.pcNumber,
dateLastRounded: moment(row.updatedAt).format("MM-D-YYYY"),
repairsNeeded: row.isPowerCordDamaged,
quarter: moment(row.updatedAt).format("Qo"),
)),
[cartsRounded]
);
return (
<div>
<IconButton
color="primary"
aria-label="new rounding"
component="span"
onClick=() =>
navigate("add_new_cart");
>
<AddCircleOutline />
</IconButton>
<div style= height: 400, width: "100%" >
<DataGrid
component=Paper
rows=rows
columns=columns
sortModel=sortModel
pageSize=100
rowsPerPageOptions=[100]
/>
</div>
</div>
);
export default Rounding;
文档示例:
_id: new ObjectId("61b95e447aec51d938e856cc"),
cartSerialNumber: 'testytitit',
pcNumber: '14124f0sdf0sfs',
isPowerCordDamaged: false,
isFuseBlown: false,
isInverterBad: false,
isInterfaceDamaged: false,
isPhysicalDamage: false,
otherNotes: '',
roundedBy: '6186c13beb18d33d5088f7b2',
createdAt: 2021-12-15T03:17:24.495Z,
updatedAt: 2021-12-15T03:17:24.495Z,
__v: 0
【问题讨论】:
【参考方案1】:我可能不理解这里的问题。但我认为您要做的是让 repairsNeeded 字段具有 Y 或 N 而不是其布尔值。
试试这个。
const rows = useMemo(
() =>
cartsRounded.map((row, index) => (
...row,
id: index,
serialNum: row.cartSerialNumber,
pcNum: row.pcNumber,
dateLastRounded: moment(row.updatedAt).format("MM-D-YYYY"),
repairsNeeded: row.isPowerCordDamaged ? "Y" : "N" // short hand for if (row.isPowerCordDamaged === true) return "Y" else return "N",
quarter: moment(row.updatedAt).format("Qo"),
)),
[cartsRounded]
);
看完之后,如果有的话,你可能想返回 Y 或 N?在这种情况下,最好将其拉入一个函数中。
const isRepairNeeded = (row) =>
if (row.isPowerCordDamaged) return "Y";
if (row.isFuseBlown) return "Y";
... rest of the ifs
return "N"; // N is only returned if none of the if statements === true;
const rows = useMemo(
() =>
cartsRounded.map((row, index) => (
...row,
id: index,
serialNum: row.cartSerialNumber,
pcNum: row.pcNumber,
dateLastRounded: moment(row.updatedAt).format("MM-D-YYYY"),
repairsNeeded: isRepairNeeded(row) // call the function will return "Y" or "N",
quarter: moment(row.updatedAt).format("Qo"),
)),
[cartsRounded]
);
【讨论】:
以上是关于地图内的 if 语句的主要内容,如果未能解决你的问题,请参考以下文章