Reputation: 1149
I have a filter's method on array, where I have a symbols of regions. Then I would like to equal to my results from HTTP request. When result is true it should console.log("profile")
, when false, should return console.log("rejected")
. But regardless of the result, there is always console.log("profile")
.getProfile(id, token)
.then(data => {
let data = data.data;
let regions = ["SP", "RS", "PR", "MG", "SC"];
if (
regions.filter(reg => {
console.log(reg);
console.log(data.region === reg);
return reg === data.region; // it is false, because data.region is AC
})
) {
console.log("profile");
} else console.log("rejected");
})
So.. I am wondering, why, when reg === data.region
is false, I have console.log("profile)
. It should be rejected. Any hint where the error lies?
Upvotes: 0
Views: 1354
Reputation: 3386
let regions = ["SP", "RS", "PR", "MG", "SC"];
let result = regions.filter(a => a === 'AC');
It give you to blank array which is not a falsy
value
so thats why if condition run...
You can use The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
let regions = ["SP", "RS", "PR", "MG", "SC"];
let result = regions.some(a => a === 'AC');
// or you can use `includes`
let result1 = regions.includes('AC');
now result contain a boolean value
You can do like this i refactor your code
.getProfile(id, token)
.then(data => {
let data = data.data;
let regions = ["SP", "RS", "PR", "MG", "SC"];
let result = regions.some(reg => reg === data.region); // it is false, because data.region is AC
if (result) {
console.log("profile");
} else {
}console.log("rejected");
})
Upvotes: 3
Reputation: 1227
filter
will always return an array. In your case, if condition is always truthy because an empty array []
is truthy.
Instead of filter
use find
this will return undefined
if nothing matched, which is falsy.
.getProfile(id, token)
.then(data => {
let data = data.data;
let regions = ["SP", "RS", "PR", "MG", "SC"];
if (
regions.find(reg => reg === data.region)
) {
console.log("profile");
} else console.log("rejected");
})
Upvotes: 1
Reputation: 15257
In addition to the other answers, if you don't need to use the filtered array but to log rejected or profile, I'd use .some()
instead of .filter()
const regions = ["SP", "RS", "PR", "MG", "SC"];
const toFind = "foo";
if (regions.some(reg => {
console.log(reg);
console.log(toFind === reg);
return toFind === reg;}))
{
console.log("profile");
}
else
console.log("rejected");
Upvotes: 1
Reputation: 38209
You can use includes
method:
if (regions.includes('AC')) {
}
or you can check length of filtered array:
let length = regions.filter(reg => {
console.log(reg);
console.log(data.region === reg);
return reg === data.region;
}).length;
if (length) {
}
Upvotes: 1
Reputation: 136239
It boils down to an empty array evaluating truthy
if([])
console.log("Empty arrays are truthy");
Fix it by checking the length
if([].length)
console.log("You wont see this.")
0
is falsey!
Upvotes: 1
Reputation: 2378
filter
method returns array, and if no matches, it returns an empty array. Even if array is empty it interpreted as true
Upvotes: 1