【问题标题】:Nested Loop through the two array for sort by category嵌套循环遍历两个数组以按类别排序
【发布时间】:2023-01-11 21:58:05
【问题描述】:

let arr = [
     {
          name: 'siggaret',
          type: 'tobbako'
     },
     {
          name: 'fanta',
          type: 'liquid'
     },
     {
          name: 'potato',
          type: 'vegetables'
     },
     {
          name: 'tvorog',
          type: 'milk'
     },
     {
          name: 'steak',
          type: 'meat'
     },
     {
          name: 'kent',
          type: 'tobbako'
     },
     {
          name: 'cola',
          type: 'liquid'
     },
     {
          name: 'potato',
          type: 'vegetables'
     },
     {
          name: 'tvorog',
          type: 'milk'
     },
     {
          name: 'steak',
          type: 'meat'
     },
     {
          name: 'sheep',
          type: 'meat'
     }
]

let categories = [
     {
          type: 'vegetables',
          arr: [],
          count: 0
     },
     {
          type: 'tobbako',
          arr: [],
          count: 0
     },
     {
          type: 'liquid',
          arr: [],
          count: 0
     },
     {
          type: 'other',
          arr: [],
          count: 0
     }
]

/*
    
*/


for (let item of arr) {
     for (let category of categories) {
          if(item.type === category.type){
               category.arr.push(item.name)
               category.count++
          } else {
               category.arr.push(item.name)
               category.count++
          }
     }
}

console.log(categories)

others 中没有添加项目?问题是什么 ?

我尝试按类别对所有项目进行排序。

嵌套循环效果不佳,但我尝试使用 for of 并且排序存在一些问题。

当我尝试按 item.name 和 category.type 排序时,所有项目的名称都会添加到所有类别中。

我有两个数组,我需要找到两者之间的区别并将它们显示在无序列表中。

我可以循环遍历主数组以获得单个匹配项,但我不知道如何循环遍历主列表以获取多个键值并有效地进行。

以下是每个数组的键值摘录:

【问题讨论】:

  • 问题不明确...预期结果是什么?按类别排序:按类别名称字母顺序排序,还是按计数排序?
  • 我尝试通过对数组项进行排序来将数组项添加到类别 arr 中。

标签: javascript arrays object nested-loops for-of-loop


【解决方案1】:

您可以将类别转换为一个对象,而不是遍历两个数组,其中类别类型是一个键,因此您可以只使用 type 作为键:

let arr = [
     {
          name: 'siggaret',
          type: 'tobbako'
     },
     {
          name: 'fanta',
          type: 'liquid'
     },
     {
          name: 'potato',
          type: 'vegetables'
     },
     {
          name: 'tvorog',
          type: 'milk'
     },
     {
          name: 'steak',
          type: 'meat'
     },
     {
          name: 'kent',
          type: 'tobbako'
     },
     {
          name: 'cola',
          type: 'liquid'
     },
     {
          name: 'potato',
          type: 'vegetables'
     },
     {
          name: 'tvorog',
          type: 'milk'
     },
     {
          name: 'steak',
          type: 'meat'
     },
     {
          name: 'sheep',
          type: 'meat'
     }
]

let categories = {
     'vegetables': {
          arr: [],
          count: 0
     },
     'tobbako': {
          arr: [],
          count: 0
     },
     'liquid': {
          arr: [],
          count: 0
     },
     'other': {
          arr: [],
          count: 0
     }
}


for (let item of arr) {
  //get category object or fallback to "other"
  const category = categories[item.type] || categories.other;
  category.arr.push(item.name)
  category.count++
}

console.log(categories)

// now we can get an array sorted by count of our categories name
const sortedByCount = Object.keys(categories).sort((a,b) => categories[b].count - categories[a].count).map(a => a + " = "  + categories[a].count);
console.log("categories sorted by count", sortedByCount);

//or convert it into array w/sorted names
const categoriesArray = Object.keys(categories).map(type =>
{
  const category = Object.assign({}, categories[type]); //clone object
//  const category = categories[type]; //don't clone object
  category.arr.sort(); //sort array of names
  category.type = type;
  return category;
});
console.log("categories as array w/sorted names", categoriesArray);
.as-console-wrapper{top:0;max-height:unset!important;overflow:auto!important;}

【讨论】:

  • 令人难以置信的是,这是正确解决 OP 请求的唯一答案。做得很好
【解决方案2】:

在您的代码中,if () { ... } else { ... } 的两个分支都运行相同的代码。您应该删除 else { ... } 分支。

像这样:

for (let item of arr) {
     for (let category of categories) {
          if (item.type === category.type){
               category.arr.push(item.name);
               category.count++;
          }
     }
}

【讨论】:

  • 我这样做了,但其他人的 arr 是空的!!!
【解决方案3】:

问题出在 else 语句中,基本上,您不需要 else 语句,因为如果您看到它们都是相同的代码,在您的情况下,您正在向类别中添加项目,因此删除 else 语句:

for (let item of arr){
   for (let category of categories) {
      if(item.type === category.type){
          category.arr.push(item.name);
           category.count++;
        }
     }
  }

【讨论】:

    【解决方案4】:

    好的开始... 问题是你的 else 声明。发生的事情是:

    else {
           category.arr.push(item.name)
           category.count++
     }
    

    ... 经常触发。每当一个类别不匹配时,它就会被推送。

    修复:您需要在该循环之外处理“推送到“其他”部分”。

    【讨论】:

    • 我怎么看得懂???
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多