1

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).

Hommer Smith
  • 26,772
  • 56
  • 167
  • 296

2 Answers2

2

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)
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

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.

John S
  • 101
  • 1