JavaScript Array Filter

未能成功加载,请稍后再试
0/0

In this lecture I'm going to show you how to filter an array based on a search criteria.

So back to our numbers array, let's say we want to return only the positive numbers.

So we can use the filter method like this.

Filter.

Once again we need to pass a callback function.

This function has three parameters value, index, and array.

So Out of this, we only care about value, we don't care about the index, because I just want to see if the number or the value is positive.

So function value.

Now here we can write a simple expression like this, if value is greater than or equal to zero.

So when we call the filter method, this method will loop through this array and execute this callback function for each number, for each element.

下载全新《每日英语听力》客户端,查看完整内容