简体   繁体   中英

Javascript filter() function does not work

I have a little function which should grab an elements of array " arr ", then create a new array " arrNew " within itself that would contain only the elements of the first array, by the condition that the variable func == true .

Function assignment is to display an elements in array arr that consist of condition arr[i] % 2 == 0.

For now function work not very good, because - console.log(arr) and the next one results are the same;

(7) [1, 2, 3, 4, 5, 6, 7] and (7) [1, 2, 3, 4, 5, 6, 7] 

.

function filter(arr, func) {
  var arrNew = [];

  for (var i = 0; i < arr.length; i++) {
    if (func = true) {
        arrNew[i] = arr[i];
    }
  }

  return arrNew;
}

var arr = [1, 2, 3, 4, 5, 6, 7];

console.log(arr);
console.log(filter(arr, function(a) {
  return a % 2 == 0;
})); // 2,4,6

-------------------------------------UPDATE----------------------------------------

I have change if (func = true) on if (func(arr[i]) == true) and now function work fine.

But I have an extra problem, when I had added an extra function in file with the first one. The first is work right, but the second make an error " func is not a function " in the first one in the string " if (func(arr[i]) == true) ". I do not understand why...

So, the displaing of first function is fine. But The second - error :( ...

function filter(arr, func) {
  var arrNew = [];

  for (var i = 0; i < arr.length; i++) {
    if (func(arr[i]) == true) {
      arrNew.push(arr[i]);
    }
  }

  return arrNew;
}

function inBetween(a, b) {
  var arrayBtw = [];

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] >= a && arr[i] <= b) {
      arrayBtw.push(arr[i]);
    }
  }

  return arrayBtw;
}

var arr = [1, 2, 3, 4, 5, 6, 7];

console.log(filter(arr, function(a) {
  return a % 2 == 0;
})); // 2,4,6

console.log( filter(arr, inBetween(3, 6)) ); // 3,4,5,6

You're doing it wrong. You have used an assignment operator in place where you should be using a comparison operator. And also you need to use arr.push() to append new elements to an array.

Your code needs to be changed as:

if (func(arr[i]) === true) {
    arrNew.push(arr[i]);
}

Since others have already pointed this out, I'll add something more. To achieve the same results, you can use the built in Javascript filter function.

var arr = [1, 2, 3, 4, 5, 6, 7];

var arrNew = arr.filter(function(a){
            return a % 2 == 0;
        });

console.log(arrNew);

Or to do the same in a more concise and a compact way, you can use an arrow function .

var arrNew = arr.filter(a => a % 2 == 0);

This one liner will yield the same result ;) Hope it helps.

=== Answer to the updated question ===

you get the error that func is not a function because in the following line, you pass inBetween(3, 6) as the second parameter to the filter function.

console.log(filter(arr, inBetween(3, 6)));

But your filter function is expecting a function type as the second parameter and your inBetween function is returning an array (instead of a " function " object). You need to return a function from the inBetween ;)

If not, and you tried to get numbers which are divisible by two in between 3 and 6, then you have passed the values in the wrong order :) change the order and it will do.

console.log(filter(inBetween(3, 6), function (a) {
    return a % 2 == 0;
})); //4,6

If you only wanted to get the numbers in between 3 to 6 (without any filtering, referring to your commented expected output), then you don't need the outer filter method call. Just remove it.

console.log(inBetween(3, 6)); // 3,4,5,6

Hope it helps :)

there is some error on the code, first is not

func = true is func(arr[i]) === true

and instead of arrNew[i] = arr[i]; you shoud arrNew.push(arr[i]);

you need to call the function and you are assigning true to the function not comparing

Instead of

if (func = true)

try

if (func(arr[i]) == true)

And instead of

arrNew[i] = arr[i];

try

arrNew.push(arr[i]);

Also note that JavaScript already has an array filter function, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter for details.

if (func = true) {

^ This is an assignment. You assign the value true to your variable func . You want to call the function, so func(arr[i]) instead of func .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM