如何限制array.filter的10个结果?

我有大数组,我想进行自动完成搜索,但我想只显示10个结果,所以在找到10个结果时停止迭代数组。我做了这个:


let items = array.filter(r => r.indexOf(term)!=-1);

console.log(items.length) // lots of items, need to be limited to 10

它有效,但我不知道如何在array.filter达到预期限制时停止它。


扬帆大鱼
浏览 606回答 9
9回答

GCT1015

基本上你可以使用一个生成器函数,它可以通过自制限制来停止,就像在下面的函数中一样function *filter(array, condition, maxSize) {&nbsp; if (!maxSize || maxSize > array.length) {&nbsp; &nbsp; maxSize = array.length;&nbsp; }&nbsp; let count = 0;&nbsp; let i = 0;&nbsp; while ( count< maxSize && i < array.length ) {&nbsp; &nbsp; if (condition(array[i])) {&nbsp; &nbsp; &nbsp; yield array[i];&nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; }&nbsp; &nbsp; i++;&nbsp; }}const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];console.log( Array.from( filter(array, i => i % 2 === 0, 2 ) ) ); // expect 2 & 4因此它会在达到maxSize作为参数后停止,并且很容易将其返回到数组中,您可以使用Array.from,它将迭代生成器函数的迭代器

江户川乱折腾

您可以移交计数器并省略任何其他值以进行过滤。const&nbsp; &nbsp; filter = v => v % 2,&nbsp; &nbsp; filterMax = (fn, c) => x => c && fn(x) && c--,&nbsp; &nbsp; max = 3,&nbsp; &nbsp; array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],&nbsp; &nbsp; result = array.filter(filterMax(filter, max));console.log(result);将Icepickle的答案提前一点,用循环找到下一个有效项并产生这个。function* filterMax(array, cb, count) {&nbsp; &nbsp; var i = 0;&nbsp; &nbsp; while (count) {&nbsp; &nbsp; &nbsp; &nbsp; while (i < array.length && !cb(array[i])) i++;&nbsp; &nbsp; &nbsp; &nbsp; if (i >= array.length) return;&nbsp; &nbsp; &nbsp; &nbsp; yield array[i++];&nbsp; &nbsp; &nbsp; &nbsp; count--;&nbsp; &nbsp; }}const&nbsp; &nbsp; filter = v => v % 2,&nbsp; &nbsp; max = 3,&nbsp; &nbsp; array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];console.log(...filterMax(array, filter, max));

慕娘9325324

您可以使用另一个变量来跟踪到目前为止与条件匹配的项目数,并且在达到限制后始终返回false。这是一个例子:const arr = [1,0,2,0,3,0,4,5,6,7,8,9,10,11,12,13,14];const filtered = arr.filter(function(item) {  if (this.count < 10 && item > 0) {    this.count++;    return true;  }  return false;}, {count: 0});console.log(filtered);在这里,我使用一个对象{count: 0}作为回调函数的上下文。

长风秋雁

只是为了诀窍:编辑:澄清此代码将选择10个第一个偶数列表let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];const result = array.reduce((temp, value) => {&nbsp; if(value%2==0 && temp.length<10)&nbsp; &nbsp; temp.push(value);&nbsp; return temp;}, []);console.log(result);

牧羊人nacy

你不能break从Array.prototype.filter方法。它将遍历每个元素。您可以使用简单的for循环并在找到10个项目时中断const items = []for (const value of array) {&nbsp; if (value.includes(term))&nbsp; &nbsp; items.push(value)&nbsp; if (items.length === 10)&nbsp; &nbsp; break;}

繁星点点滴滴

您可以定义自定义方法,Array.prototype其中将包含2个参数。一个回调和结果数组的最大元素将包含。下面的代码从数组中获取前3个奇数。function filterUpto(callback,max){&nbsp; let len = this.length&nbsp; let res = [];&nbsp; let i = 0;&nbsp; while(res.length < max && i < len){&nbsp; &nbsp; if(callback(this[i],i,this)) res.push(arr[i])&nbsp; &nbsp; i++&nbsp; }&nbsp; return res;}Object.defineProperty(Array.prototype,'filterUpto',{&nbsp; value:filterUpto})let arr = [1,2,3,4,5,6,7,8,9,10];console.log(arr.filterUpto(x => x % 2,3)); //first three odd numbers

ABOUTYOU

var data = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14"]var limited = data.filter((val,i)=>i<10)console.log(limited)

UYOU

你无法阻止/破坏中间的过滤器。您可以做的是编写传统的for循环来遍历项目,然后在找到前10时中断。我对这个问题的看法:let items = [];let required_count = 10;let index = 0;do{&nbsp; if(array[index].indexOf(term) != -1){&nbsp; &nbsp; items.push(array[index])&nbsp; }index += 1;}while(items.length < required_count && index < array.length);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript