0

I am trying to filter an array as I have a million times in the past but it's not working.

locations = city.locations.filter(
   (l) => {
     console.log(l._id);
     console.log(req.query.locationId);
     console.log(l._id === req.query.location);
     return l._id === req.query.location;
   }
)

Console output:

l1dr2jmg42lb2sgiudi
l1dr2jmg42lb2sgiudi
false

The first two console.log print identical strings but the third prints false??!?

I checked that city.locations is an Array and both l._id and req.query.locationId are a string.

8
  • Show your source data, actual results and desired results. Otherwise your question is off-topic. Commented Nov 16, 2016 at 10:14
  • Printing the string can hide undesired spaces before or after it. To make sure try console.log('"' + l._id + '"'). It is silly, but it happens. Commented Nov 16, 2016 at 10:15
  • @hindmost added the console output Commented Nov 16, 2016 at 10:18
  • @DontVoteMeDown I checked for spaces. None there. Makes no sense! Commented Nov 16, 2016 at 10:18
  • Out of interest if you change === to == in the 3rd console.log what do you get? Commented Nov 16, 2016 at 10:19

1 Answer 1

3

console.log(l._id === req.query.location);

you are not referring to req.query.locationId but toreq.query.location. Missing Id.

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

3 Comments

Oh god! Let this SO page be witness of my shame for posterity!
@Mika perhaps its time for a break, go get some sleep. haha
Experience says that computers don't fail at string comparison, but humans do.

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.