首页 > 解决方案 > 根据属性值限制数组中的对象

问题描述

我有一个如下所示的数组。我需要将数组中的对象数量限制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"}}

]

标签: javascriptarrays

解决方案


您可以创建以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);


推荐阅读