javascript - 如何使用对象数组访问 if?
问题描述
您好,我是 JS 的新手,我需要测试 if。
const length = notifications.length
notifications.forEach((notification, index) => {
if (length > 1 && index < length - 1) {
toolTipText += ' '
}
换句话说,我需要声明一个变量来输入 if。我有这些变量,但是错了,我不知道如何做好
const mockPropsForComponentAlternatorAndLOW = {
notifications: [{
params: {
subType: 'ALTERNATOR'
}
},
params: {
subType: 'LOW'
}]
}
有什么建议么?
解决方案
您的脚本有效。只需删除一些语法错误并指向正确的参考:
mockPropsForComponentAlternatorAndLOW.notifications.length
const mockPropsForComponentAlternatorAndLOW = {
notifications: [
{
params: {
subType: 'ALTERNATOR'
}
},
{
params: {
subType: 'LOW'
}
}
]
}
const length = mockPropsForComponentAlternatorAndLOW.notifications.length
mockPropsForComponentAlternatorAndLOW.notifications.forEach((notification, index) => {
if (length > 1 && index < length - 1) {
alert('in the scope now')
// toolTipText += ' '
}
})
推荐阅读
- reactjs - 一起使用多个 `useState()` 状态
- javascript - 为等待 .. 脱糖
- javascript - 仅使用 JavaScript 更快的数字计数器
- ruby-on-rails - 我应该在 Rails 中使用多个 Action Mailer 吗?
- java - JSON数据在Intellij的输出控制台中显示为一行,放心
- python - For 循环不排序 +=1
- javascript - SVG元素中没有显示文本?
- pine-script - TradingView Pine alertcondition 基于颜色变化
- python-3.x - Raspberry Pi 似乎认为按钮一直被按下
- reactjs - React Rest Api 调用模式