首页 > 解决方案 > 如何使用对象数组访问 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'
    }]
}

有什么建议么?

标签: javascript

解决方案


您的脚本有效。只需删除一些语法错误并指向正确的参考:

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 += ' '
  }
})


推荐阅读