簡體   English   中英

根據屬性值限制數組中的對象

[英]Limit objects in array based on property value

我有一個像下面所示的數組。 我需要使用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":1 ,並在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"}} ], output = [], count = 0; input.forEach( obj => { if(obj.model.ord===1){ if(count>=5) return count++ } output.push(obj) }) console.log("Count before : " + input.filter(o=>o.model.ord===1).length ) console.log("Count after : " + output.filter(o=>o.model.ord===1).length ) 

您可以使用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); 

為了達到預期的效果,請使用過濾方法來過濾順序:1記錄和切片數組方法獲得前5個

let final = arr.filter(v => v.model.ord === 1).slice(0, 5);

codepen- https: //codepen.io/nagasai/pen/NOmXar ? editors = 1010

 let arr = [{"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"}}] let final = arr.filter(v => v.model.ord === 1).slice(0, 5); console.log(final) 

您可以創建類似於我在下面作為tracker 使用該變量可以按ord計數項目。 在下面的示例中,我在下面的原始數據數組上使用Array.prototype.filter 每次回調遇到ord屬性等於5的數組元素時, tracker計數都會增加。 如果計數少於5,我們可以將其添加到新數組中,否則請跳過它。

 var tracker = (function(i) { var c = i; return { value: () => c, increment: () => c += 1, decrement: () => c -= 1 } })(0); var data = [{ id: 'item0', ord: 1 }, { id: 'item1', ord: 1 }, { id: 'item2', ord: 1 }, { id: 'item3', ord: 1 }, { id: 'item4', ord: 1 }, { id: 'item5', ord: 1 }, { id: 'item6', ord: 1 }, { id: 'item7', ord: 1 }, { id: 'item8', ord: 1 }, { id: 'item9', ord: 2 }, { id: 'item10', ord: 2 }, { id: 'item11', ord: 2 }, { id: 'item12', ord: 2 }]; var limited = data.filter(el => { if (el.ord === 1) { tracker.increment(); if (tracker.value() < 6) { return true; } return false; } return true; }); console.log(limited); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM