【发布时间】:2018-10-29 14:04:00
【问题描述】:
我有一个如下所示的数组。我需要将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"}}
]
【问题讨论】:
-
你是什么意思限制?防止添加?从太多减少?警告?
-
@isherwood 我将以此创建新数组,在结果数组中我需要 olly 5 个对象,其中包含 ord:1
-
还是不清楚。您可以通过格式化上述内容来帮助人们,以便使用在线 JSON 格式化程序对其进行读取。您还可以包含预期结果的示例。
-
您的脚本无效,它包含未终止的字符串。
标签: javascript arrays