【问题标题】:Increment by 1 when using .map and a condition evaluates to true使用 .map 且条件评估为 true 时递增 1
【发布时间】:2019-09-20 03:45:00
【问题描述】:

当我使用.map 循环遍历数组myClasses 并且当数组中的status 等于“打开”或“进行中”时,我很难将数字加1。我的目标是拥有“开放”或“进行中”的课程总数

这是我目前正在做的事情

myClasses.map(a => (a.status === ('Open' || 'In Progress') ? 'Yes' : 'No'))

目前,当课程打开或进行中时,我只输出 yesno,但我希望能够将 1 或 0 添加到数字变量。

这是 myClasses 数组包含的内容

0: {id: "0000AK",  type: "Elective", status: "Open"       } 
1: {id: "597WUK",  type: "Core",     status: "Closed"     } 
2: {id: "082HCE",  type: "Core",     status: "In Progress"} 
3: {id: "091HSN",  type: "Elective", status: "Canceled"   } 
4: {id: "038GAQ",  type: "Core",     status: "Open"       } 

现在我的输出看起来像这样 Yes,No,Yes,No,Yes 而我希望输出是 3

有什么建议吗?

【问题讨论】:

  • ('Open' || 'In Progress') 将始终评估为 "Open" 因为它是一个真实值。你应该使用if(['Open', 'In Progress'].includes(a.status))
  • 您的条件a.status === ('Open' || 'In Progress') 是错误的。改写为(a.status === 'Open' || a.status === 'In Progress')
  • 如果你想得到一个单一的值,为什么你需要拿地图?
  • 除了其他人建议的对比较的更改之外,您还应该在这里使用filter()而不是map()。那么计数就是结果数组的长度。或者,您可以使用reduce() 对计数器求和。

标签: javascript array.prototype.map


【解决方案1】:

如果它只是您所追求的计数,您可以简单地使用 filter 并返回数组的 length

const myClasses = [{id:"0000AK",type:"Elective",status:"Open"},{id:"597WUK",type:"Core",status:"Closed"},{id:"082HCE",type:"Core",status:"In Progress"},{id:"091HSN",type:"Elective",status:"Canceled"},{id:"038GAQ",type:"Core",status:"Open"}];

const count = myClasses.filter(a => a.status === 'Open' || a.status === 'In Progress').length

console.log(count)

【讨论】:

    【解决方案2】:

    您可以将计数计入三进制的真实部分。

    var myClasses = [{ id: "0000AK", type: "Elective", status: "Open" }, { id: "597WUK", type: "Core", status: "Closed" }, { id: "082HCE", type: "Core", status: "In Progress" }, { id: "091HSN", type: "Elective", status: "Canceled" }, { id: "038GAQ", type: "Core", status: "Open" }],
        count = 0,
        result = myClasses.map(({ status }) => status === 'Open' || status === 'In Progress'
            ? (count++, 'Yes')
            : 'No'
        );
    
    console.log(count);
    console.log(result);

    要获得计数,您可以减少数组。如果你想检查更多的值,你可以取一个数组或Set 并检查数据结构是否包含一个元素。

    var myClasses = [{ id: "0000AK", type: "Elective", status: "Open" }, { id: "597WUK", type: "Core", status: "Closed" }, { id: "082HCE", type: "Core", status: "In Progress" }, { id: "091HSN", type: "Elective", status: "Canceled" }, { id: "038GAQ", type: "Core", status: "Open" }],
        wanted = ['Open', 'In Progress'],
        count = myClasses.reduce((c, { status }) => c + wanted.includes(status), 0);
    
     console.log(count);

    【讨论】:

      【解决方案3】:

      怎么样?数组减少

      const data = [
        { id: '0000AK', type: 'Elective', status: 'Open' },
        { id: '597WUK', type: 'Core', status: 'Closed' },
        { id: '082HCE', type: 'Core', status: 'In Progress' },
        { id: '091HSN', type: 'Elective', status: 'Canceled' },
        { id: '038GAQ', type: 'Core', status: 'Open' }
      ];
      
      const res = data.reduce((acc, cur) => {
        const status = cur.status;
        if (status === 'Open' || status === 'In Progress') {
          return acc + 1;
        }
        return acc;
      }, 0);
      console.log(res);
      

      【讨论】:

        【解决方案4】:

        我建议你使用reduce() 而不是map()

        var count = myClasses.reduce((accumulator, item) => {
            if (item.status == 'Open' || item.status == 'In Progress') {
                return accumulator + 1;
            }
            return accumulator;
        }, 0);
        

        请注意,我还修改了if 语句中的条件,以便与您想要的两个值进行比较。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-03-06
          • 1970-01-01
          • 2012-01-24
          • 2021-10-16
          • 1970-01-01
          • 2017-05-17
          相关资源
          最近更新 更多