JAVASCRIPT: return statement in normal vs arrow functions.
Go to solution
Solved by Vicarian,
8 hours ago, shivajikobardan said:Source code: https://flexiple.com/javascript/javascript-filter-array/
let freelancers = [{name: "Harry", skill: "JavaScript"},{name: "Mark", skill: "Python"},{name: "David", skill:"JavaScript"}]; let javascript_freelancers = freelancers.filter(function(freelancer) { return freelancer.skill == "JavaScript"; }); console.log(javascript_freelancers);
My code:let freelancers = [{ name: "Harry", skill: "JavaScript" }, { name: "Mark", skill: "Python" }, { name: "David", skill: "JavaScript" }]; let newfreelancers = freelancers.filter((obj) => obj.skill == "Javascript"); console.log(newfreelancers); My code works when I remove 1 "=" from obj.skill=="Javascript". Why it's like that?My code works when I remove 1 "=" from obj.skill=="Javascript". Why it's like that?
String comparisons are case sensitive. You'd want to include a toUpperCase() or toLowerCase() call on the skill property and compare against an appropriate string literal to make sure you're being case-insensitive. Put a different way, your data has the string "JavaScript" but you're filtering for "Javascript" The strings don't match. The reason it works with the single = sign is because you're assigning the data, not reading it. With the assignment, you'll notice that Mark suddenly knows how to do Javascript, when he should have Python.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now