首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除数组中的多个对象?

如何删除数组中的多个对象?
EN

Stack Overflow用户
提问于 2022-07-10 22:57:25
回答 2查看 35关注 0票数 -1

我有这样一个数组:

代码语言:javascript
复制
const myArray = [ 
  {
    title: 'title',
    category: 'genetic-diseases'
  },
  {
    title: 'title',
    category: 'genetic-disorders'
  },
  { title: 'title', category: 'genetic-disorders' },
  {
    title: 'title',
    category: 'genetic-disorders'
  },
  {
    title: 'title',
    category: 'genetic-mutation'
  },
  {
    title: 'title',
    category: 'genetic-mutation'
  },
  {
    title: 'title',
    category: 'genetic-mutation'
  }
]

我只想要数组中每个类别对象中的两个,如果该类别的对象数超过2,则删除其余的对象,并得到如下所示:

代码语言:javascript
复制
const myArray = [ 
  {
    title: 'title',
    category: 'genetic-diseases'
  },
  { title: 'title', category: 'genetic-disorders' },
  {
    title: 'title',
    category: 'genetic-disorders'
  },
  {
    title: 'title',
    category: 'genetic-mutation'
  },
  {
    title: 'title',
    category: 'genetic-mutation'
  }
]

如果数组中的对象数量超过2,如何删除特定类别的其余对象?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-10 23:14:26

代码语言:javascript
复制
    const myArray = [ 
    {
    title: 'title',
    category: 'genetic-diseases'
    },
    {
    title: 'title',
    category: 'genetic-disorders'
    },
    { title: 'title', category: 'genetic-disorders' },
    {
    title: 'title',
    category: 'genetic-disorders'
    },
    {
    title: 'title',
    category: 'genetic-mutation'
    },
    {
    title: 'title',
    category: 'genetic-mutation'
    },
    {
    title: 'title',
    category: 'genetic-mutation'
    }
    ];
    
    var categoryQuantities = {}; //counts how many objects from each category entered the filtered array
    var filteredArray = []; //array with first 2 objects from each categoty
    for (var obj of myArray) {
        var category = obj.category;
        if (category in categoryQuantities) {
            categoryQuantities[category]++;
        } else {
            categoryQuantities[category] = 1;
        }
        
        if (categoryQuantities[category] <= 2) {
            filteredArray.push(obj);
        }
    }
    
    console.log(filteredArray);

最后一个数组位于'filteredArray‘中。原始数组没有更改,因此您可能要执行以下操作:

代码语言:javascript
复制
    myArray = filteredArray;
票数 1
EN

Stack Overflow用户

发布于 2022-07-10 23:15:53

基本上我做了两个阶段。首先,我按类别分组(每个类别最多2项),然后我将其夷平。我把它变成了一个名为group_by_at_most的通用函数

代码语言:javascript
复制
var myArray = get_data();

var result = group_by_at_most(myArray, 'category', 2);
console.log(result);

function group_by_at_most(myArray, by, at_most) {
  var obj = {}

  myArray.forEach(function(item) {
    if (!obj[item[by]]) {
      obj[item[by]] = []
    }
    if (obj[item[by]].length < at_most) {
      obj[item[by]].push(item);
    }
  })

  var result = [];
  for (var key in obj) {
    var items = obj[key];
    result.push(...items);
  }

  return result;
}



function get_data() {
  return [{
      title: 'title',
      category: 'genetic-diseases'
    },
    {
      title: 'title',
      category: 'genetic-disorders'
    },
    {
      title: 'title',
      category: 'genetic-disorders'
    },
    {
      title: 'title',
      category: 'genetic-disorders'
    },
    {
      title: 'title',
      category: 'genetic-mutation'
    },
    {
      title: 'title',
      category: 'genetic-mutation'
    },
    {
      title: 'title',
      category: 'genetic-mutation'
    }
  ]
}
代码语言:javascript
复制
.as-console-wrapper {
  max-height: 100% !important;
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72932402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档