Reputation: 27852
I have the following filter:
const auxHash = {};
myArray.filter(house =>
house.members === 3 &&
auxHash[house.id] ? false : auxHash[house.id] = true
)
And I get a lint error saying that an arrow function should not return an assignment. I have tried this too:
const auxHash = {};
myArray.filter(house =>
house.members === 3 &&
auxHash[house.id] ? false : (auxHash[house.id] = true)
)
But same problem. How can I solve this?
To clarify. I am trying to filter out elements of an array which attribute members
is different than 3
, and also I am trying to remove duplicate elements based on the attribute id
(that's why I am using the hash).
Upvotes: 1
Views: 2388
Reputation: 101
You're reformatting the exact same statement and ignoring the linter's error. You are returning an assignment. That is your problem. You're doing it right here - auxHash[house.id] = true
Your ternary resolves to false or auxHash[house.id] = true
So, refactor to return an actual value -
const auxHash = {};
myArray.filter(house => {
if(house.members === 3 && auxHash[house.id]) {
return false;
} else {
auxHash[house.id] = true;
return true;
}
});
You can write it more concisely but I stretched it out to make it clear.
Upvotes: 0
Reputation: 68665
Before question edit.
If you use {}
body you need explicitly return a result from that function.
const auxHash = {};
myArray.filter(house => {
return house.members === 3 && auxHash[house.id] ? false : auxHash[house.id] = true;
})
Also, if your statement is one line, you can omit the {}
part and remove return
const auxHash = {};
myArray.filter(house => house.members === 3 && auxHash[house.id] ? false : auxHash[house.id] = true)
Upvotes: 2