【问题标题】:Hot Categories / inventory checker热门类别/库存检查器
【发布时间】:2021-08-03 09:17:38
【问题描述】:

我们在学校进行了这项活动,以寻找没有更多库存的类别。热门类别应该是唯一的,并且没有重复的类别。

const items = [
        { id: 'tltry001', name: 'soap', stocks: 14, category: 'toiletries' },
        { id: 'tltry002', name: 'shampoo', stocks: 8, category: 'toiletries' },
        { id: 'tltry003', name: 'tissues', stocks: 0, category: 'toiletries' },
        { id: 'gdgt001', name: 'phone', stocks: 0, category: 'gadgets' },
        { id: 'gdgt002', name: 'monitor', stocks: 0, category: 'gadgets' }
    ];

到目前为止,这是我的代码。我只得到一个类别,无法显示 0 个股票的第二个类别。

function findHotCategories(items) {
    
     let ilen = items.length
     for(let i = 0; i < ilen; i++){
       if(items[i].stocks === 0){
         let newArr = items[i].category
            return [newArr]
       }
    }
  };

【问题讨论】:

  • return 有什么作用?这能回答你的问题吗?
  • 对不起,我不明白。

标签: javascript arrays function


【解决方案1】:

// dont forget to UpVoted (:

const items = [
        { id: 'tltry001', name: 'soap', stocks: 14, category: 'toiletries' },
        { id: 'tltry002', name: 'shampoo', stocks: 8, category: 'toiletries' },
        { id: 'tltry003', name: 'tissues', stocks: 0, category: 'toiletries' },
        { id: 'gdgt001', name: 'phone', stocks: 0, category: 'gadgets' },
        { id: 'gdgt002', name: 'monitor', stocks: 0, category: 'gadgets' }
    ];

const emptyStocks = [];
for( let a in items ){ if( items[a].stocks < 1 ) emptyStocks.push(items[a]); }
console.log( emptyStocks );

【讨论】:

    【解决方案2】:

    return 在循环中,所以函数在它与第一项匹配的地方结束。

    你应该在循环之后返回

    const items = [
            { id: 'tltry001', name: 'soap', stocks: 14, category: 'toiletries' },
            { id: 'tltry002', name: 'shampoo', stocks: 8, category: 'toiletries' },
            { id: 'tltry003', name: 'tissues', stocks: 0, category: 'toiletries' },
            { id: 'gdgt001', name: 'phone', stocks: 0, category: 'gadgets' },
            { id: 'gdgt002', name: 'monitor', stocks: 0, category: 'gadgets' }
        ];
    
    function findHotCategories(items) {
      let newArr = [];    
         let ilen = items.length
         for(let i = 0; i < ilen; i++){
           if(items[i].stocks === 0){
             newArr.push(items[i].category)
           }
        }
        return [...new Set(newArr)];
      }
      
    console.log(findHotCategories(items));

    【讨论】:

    • 谢谢。但它给出了重复的类别。
    • 对不起,我忽略了你的目标。已更新。
    【解决方案3】:

    这里是:

    const items = [
            { id: 'tltry001', name: 'soap', stocks: 14, category: 'toiletries' },
            { id: 'tltry002', name: 'shampoo', stocks: 8, category: 'toiletries' },
            { id: 'tltry003', name: 'tissues', stocks: 0, category: 'toiletries' },
            { id: 'gdgt001', name: 'phone', stocks: 0, category: 'gadgets' },
            { id: 'gdgt002', name: 'monitor', stocks: 0, category: 'gadgets' }
        ];
    
    const totalStocks = items.reduce((acc, prev) => {
        if (!acc[prev.category]) {
            return { ...acc, [prev.category]: prev.stocks }
        }
        return { ...acc, [prev.category]: acc[prev.category] + prev.stocks }
    }, {})
    
    const emptyStocks = Object.keys(totalStocks).filter(key => totalStocks[key] === 0)
    
    console.log(emptyStocks)

    【讨论】:

      【解决方案4】:

      我对使用filtermap 函数的简短回答。

      function findHotCategories(items) {
        const result = items.filter(e => e.stocks === 0).map(e => e.category);
        return [...new Set(result)];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-17
        • 2017-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        相关资源
        最近更新 更多