2

I have a program that adds or removes cars to an array called pile so that the cars can later be selected based on filters and criteria. The code works as it is but I am trying to increase it's efficiency for later changes. Below is code to show what my question is:

if ( var1 == true ) {
  pile.push(car1);
} else if ( var1 == false ) {
  pile = pile.filter( removeCar );
}

function removeCar(elem) {
  return String(elem[0][0]) !== 'Jeep';
}

What I want to do is for removeCar() to have two parameters so it is removeCar(elem, make) such that if make = 'Jeep' the code would function the same way. Unfortunately, as you can see removeCar is called by filter with no parameters and elem is automatically assigned to the current element.

How can I add parameters to the .filter( removeCar)?

6
  • 1
    interesting, you assign true to the variables and do not make a check with it. Commented May 13, 2020 at 15:47
  • 2
    I don't think this code works. If(var1 = true) will always be true because thats assignment and you will never get to the else. Also in the else you don't need to test if its false again because if it's not true it will be false assuming that its a boolean ofcourse. Now you can pass in values in to a function by so pile = pile.filter( elem => removeCar(elem) ); Commented May 13, 2020 at 15:52
  • yea I just didn't copy that part correctly because it is more complicated and hides the meaning of the question which is about the filter part. the question is fixed. Commented May 13, 2020 at 15:53
  • Okay great, that fixes it. In JS when checking for equality though it's always good to use three === but this would work Commented May 13, 2020 at 15:58
  • @Nsoseka technically you gave me the correct answer first but as a comment, but Dima posted the correct answer as an answer, so if you add your comment as an answer I'll mark it as the solution but if not I'll just mark Dimas Commented May 13, 2020 at 15:59

4 Answers 4

1

You can include parameters in the filter if you structure the statement differently: pile = pile.filter( elem => removeCar(elem, make) );

Sign up to request clarification or add additional context in comments.

Comments

1
pile = pile.filter( x => removeCar(x) );

Try this. If removeCar(x) return true, x(current element of pile) will be added to new pile array

And in your if/else must be '==' or '===' not '='

Comments

0

How about this:

if (var1) pile.push(car1);
pile = filterPile(pile, make);

function filterPile(pile, make){
 if(!pile) pile;
 return pile.filter(val=> val[0][0].toString() !== make)
} 

Comments

0

You can define custom filter functions by wrapping the native Array filter() method:

// A custom filter function which accepts a 'pile' and a 'make' parameter.
function removeFromPileByMake(pile, make) {
    // Assuming every element holds an object at [0]
    return pile.filter(elem => elem[0][0] !== make);
}

var make = 'Jeep';

if ( var1 ) {
    pile.push(car1);
} else {
    pile = removeFromPileByMake(pile, make);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.