javascript - 根据属性值限制数组中的对象
问题描述
我有一个如下所示的数组。我需要将数组中的对象数量限制ord:1
为 5。如何根据ord
属性限制数组中的对象数量?
[{"id":"typeahead-86-4951-option-0","label":"Spigen Samsung GS5
Case","model":{"ord":1,"short_description":"Samsung Galaxy S5"}},
{"id":"typeahead-86-4951-option-1","label":"Spigen iPhone 5/5s
Case","model":{"ord":1,"short_description":"iPhone 5 and 5s"}},
{"id":"typeahead-86-4951-option-2","label":"Earphones","model":
{"ord":1,"short_description":"Buy earphones"}},
{"id":"typeahead-86-4951-option-5","label":"Web Conferencing","model":
{"ord":1,"short_description":"Request"}},
{"id":"typeahead-86-4951-option-6","label":"Dreamweaver","model":
{"ord":1,"short_description":null}},
{"id":"typeahead-86-4951-option-7","label":"SSL Certification","model":
{"ord":1,"short_description":"Do you need to update"}},
{"id":"typeahead-86-4951-option-8","label":"Access","model":
{"ord":1,"short_description":"Microsoft Access"}},
{"id":"typeahead-86-4951-option-9","label":"Fireworks","model":
{"ord":1,"short_description":"Adobe Systems Fireworks"}},
{"id":"typeahead-86-4951-option-10","label":"Spigen iPhone 6 Case","model":
{"ord":1,"short_description":"For iPhone 6"}},
{"id":"typeahead-86-4951-option-11","label":"What is a cookie?
\t\t","model":{"ord":4,"short_description":"What is a cookie?\t\t"}},
{"id":"typeahead-86-4951-option-12","label":"What are phishing scams and
how can I avoid them?\n\t\t","model":{"ord":4,"short_description":"What
are phishing"}},
{"id":"typeahead-86-4951-option-13","label":"How to Deal with
Spam","model":{"ord":4,"short_description":"How to Deal with Spam"}},
{"id":"typeahead-86-4951-option-14","label":"What is Spam?","model":
{"ord":4,"short_description":"What is Spam?}},
{"id":"typeahead-86-4951-option-15","label":"How to set\n\t\t","model":
{"ord":4,"short_description":"How\n\t\t"}}
]
解决方案
您可以创建以ord
值作为键的字典并在其中存储最多 5 个项目。
最后只是连接字典中的所有数组。
let input = [{
"id": "typeahead-86-4951-option-0",
"label": "Spigen Samsung GS5 Case",
"model": {
"ord": 1,
"short_description": "Samsung Galaxy S5"
}
}, {
"id": "typeahead-86-4951-option-1",
"label": "Spigen iPhone 5/5s Case",
"model": {
"ord": 1,
"short_description": "iPhone 5 and 5s"
}
},
{
"id": "typeahead-86-4951-option-2",
"label": "Earphones",
"model": {
"ord": 1,
"short_description": "Buy earphones"
}
},
{
"id": "typeahead-86-4951-option-5",
"label": "Web Conferencing",
"model": {
"ord": 1,
"short_description": "Request"
}
},
{
"id": "typeahead-86-4951-option-6",
"label": "Dreamweaver",
"model": {
"ord": 1,
"short_description": null
}
},
{
"id": "typeahead-86-4951-option-7",
"label": "SSL Certification",
"model": {
"ord": 1,
"short_description": "Do you need to update"
}
},
{
"id": "typeahead-86-4951-option-8",
"label": "Access",
"model": {
"ord": 1,
"short_description": "Microsoft Access"
}
},
{
"id": "typeahead-86-4951-option-9",
"label": "Fireworks",
"model": {
"ord": 1,
"short_description": "Adobe Systems Fireworks"
}
},
{
"id": "typeahead-86-4951-option-10",
"label": "Spigen iPhone 6 Case",
"model": {
"ord": 1,
"short_description": "For iPhone 6"
}
},
{
"id": "typeahead-86-4951-option-11",
"label": "What is a cookie?\t\t",
"model": {
"ord": 4,
"short_description": "What is a cookie?\t\t"
}
},
{
"id": "typeahead-86-4951-option-12",
"label": "What are phishing scams and how can I avoid them?\n\t\t",
"model": {
"ord": 4,
"short_description": "What are phishing"
}
},
{
"id": "typeahead-86-4951-option-13",
"label": "How to Deal with Spam",
"model": {
"ord": 4,
"short_description": "How to Deal with Spam"
}
},
{
"id": "typeahead-86-4951-option-14",
"label": "What is Spam?",
"model": {
"ord": 4,
"short_description": "What is Spam?"
}
},
{
"id": "typeahead-86-4951-option-15",
"label": "How to set\n\t\t",
"model": {
"ord": 4,
"short_description": "How\n\t\t"
}
}
],
ordObj;
ordObj = input.reduce(function(acc, el) {
let ord = el.model.ord;
if (!acc.hasOwnProperty(ord)) {
acc[ord] = [];
}
if (acc[ord].length < 5) {
acc[ord].push(el);
}
return acc;
}, {});
let result = Object.values(ordObj).reduce((acc, el) => (acc.concat(el)), []);
console.log(result);
推荐阅读
- python - FastAPI 允许 openAPI 规范中的特定参数值
- android - 用于自定义对象的 Room Db TypeConverter
- c# - ASP.NET Core Web API - 功能 MapControllerRoute 的问题
- java - 当我缩放图像时,我会在它上面得到一个红色层。并非所有图像都发生这种情况
- java - 如何使用 exoplayer Ima 扩展获取随播广告的高度和宽度?
- r - IR ggplot2 , 'vjust' 和 'nudge_y' 可以调整垂直轴的文本位置。任何人都可以分辨出这两个参数的区别
- excel - 解析器 - 找到错误的 FCS
- c# - 通过 ClickOnce 发布 .NET Core WPF 应用程序时缺少设置/安装文件
- javascript - 如何使数据以百分位数为单位?
- sql - 在oracle sql中查看创建表的CTAS查询