我有一个大数组,我想进行自动完成搜索,但是我只想显示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
。
答案 0 :(得分:4)
您可以使用另一个变量来跟踪到目前为止有多少项目符合条件,并且在达到限制后始终返回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}
作为回调函数的上下文。您可以找到有关Array.filter
from here
答案 1 :(得分:3)
您可以移交计数器,并忽略其他任何要过滤的值。
const
filter = v => v % 2,
filterMax = (fn, c) => x => c && fn(x) && c--,
max = 3,
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
result = array.filter(filterMax(filter, max));
console.log(result);
稍微考虑一下Icepickle的answer的想法,然后再找到一个寻找下一个有效项目并产生该项目的循环。
function* filterMax(array, cb, count) {
var i = 0;
while (count) {
while (i < array.length && !cb(array[i])) i++;
if (i >= array.length) return;
yield array[i++];
count--;
}
}
const
filter = v => v % 2,
max = 3,
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(...filterMax(array, filter, max));
答案 2 :(得分:3)
基本上,您可以使用生成器函数,该函数可以通过自己设置的限制来停止,就像下面的函数一样
function *filter(array, condition, maxSize) {
if (!maxSize || maxSize > array.length) {
maxSize = array.length;
}
let count = 0;
let i = 0;
while ( count< maxSize && i < array.length ) {
if (condition(array[i])) {
yield array[i];
count++;
}
i++;
}
}
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,它将迭代生成器函数的迭代器
答案 3 :(得分:2)
I wrote a library对于这类事情非常有用。
在这里,我会找到以字符“ 1”开头的前100个数字
const {blinq, range} = window.blinq;
//create a large array of strings to search
const arrToBeSearched = range(0,10000)
.select(x => `${x}`)
.toArray()
const query = blinq(arrToBeSearched)
.where(x => x.startsWith("1"))
.takeWhile((x, i) => i < 100)
const result = [...query] //no calculation until we materialize on this line
console.log(result)
<script src="https://cdn.jsdelivr.net/npm/blinq"></script>
答案 4 :(得分:1)
您无法通过break
方法来Array.prototype.filter
。它将遍历每个元素。您可以使用简单的for
循环并在找到10个项目时中断
const items = []
for (const value of array) {
if (value.includes(term))
items.push(value)
if (items.length === 10)
break;
}
答案 5 :(得分:1)
仅此而已:
编辑:要澄清此代码,请选择列表中的前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) => {
if(value%2==0 && temp.length<10)
temp.push(value);
return temp;
}, []);
console.log(result);
答案 6 :(得分:0)
您可以在Array.prototype
上定义自定义方法,该方法将带有2个参数。回调和结果数组将包含的最大元素。
下面的代码从数组中获取前3个奇数。
function filterUpto(callback,max){
let len = this.length
let res = [];
let i = 0;
while(res.length < max && i < len){
if(callback(this[i],i,this)) res.push(arr[i])
i++
}
return res;
}
Object.defineProperty(Array.prototype,'filterUpto',{
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
答案 7 :(得分:0)
我知道它有点晚了,但这是为新人准备的!
// we'll create a function which will take two arguments
// first argument would be your original array which your want to filter from
// second argument would be the number of results you want the filter to return
const limitedArray = (originalArray, limit) => {
let newArray = [];
for (let item of originalArray) {
if (newArray.length >= limit) break;
//your code here
//in my case i'll jush push in to the array
newArray.push(item)
}
return newArray;
};
//---------------->ignore v<-------------------
//the above function would return an array so in other words we can see this function as an array
const array = [1, 2, 3, 4, 5, 6, 'cascas', 'ascasc', 9, 10, 'ascs'];
console.log(limitedArray(array, 4));
//similarly
limitedArray(array, 4).forEach(item => {
console.log(item)
})
答案 8 :(得分:-1)
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)
答案 9 :(得分:-1)
您可以执行此操作,只需简单地添加.Slice(0,NO_OF_ELE_WANT)
例如。找到前两个甚至没有
[1,2,3,4,5,6,7,8,9,10].filter((e)=> e%2==0).slice(0,2)
答案:let items = array.filter(r => r.indexOf(term)!=-1).slice(0,10);