0

I am running this function on TypeScript and I get this error

`Left side of comma operator is unused and has no side effects`
function groupBy(data) {

  const mapper = new Map()
    
  for (const { Name, Department } of data) {
    if (!mapper.has(Department))
     mapper.set(Department, { title: Department, people: [] })
    mapper.get(Department).people.push(Name)
  }

 return Array.from(mapper, ([title, people]) => (title, people))
}

All of this works fine on normal JS so there must be something about TS

Playground link

Working on regular JS

const data = [{ Name: 'Peter',Department: 'Finance'},{Name: 'Jane',Department: 'HR'},{Name: 'Tom',Department: 'Finance'}]

function groupBy(data) {

  const mapper = new Map()
  for (const { Department, Name} of data) {
    if (!mapper.has(Department)) mapper.set(Department, { title: Department, people: []}) 
    mapper.get(Department).people.push(Name)
  }

  return Array.from(mapper, ([title, people]) => (title, people))
}

console.log(groupBy(data))
.as-console-wrapper { max-height: 100% !important; top: 0; }
Álvaro
  • 835
  • 4
  • 12
  • 1
    `(title, people)`? Should this be `({title, people})` or what? – VLAZ Mar 17 '21 at 15:49
  • Of the return array, is what the for loop does, creates an array of objects `{ title: string, people: []}` They are contained in the `mapper` Map object – Álvaro Mar 17 '21 at 15:51
  • 3
    Also shouldn't `mapper.get(Department).push(Name)` be `mapper.get(Department).people.push(Name)`? – VLAZ Mar 17 '21 at 15:53
  • Oh yes, you are right, that fixed the second problem – Álvaro Mar 17 '21 at 15:55
  • _All of this works fine on normal JS so there must be something about TS_ No, it doesn't work in "normal JS" – Aleksey L. Mar 17 '21 at 15:56
  • Now it does, made a mistake sorry – Álvaro Mar 17 '21 at 15:57
  • @AlekseyL. I agree, neither [the comma operator version](https://jsbin.com/dixavawaqi/1/edit?js,console), nor [the one that returns an object](https://jsbin.com/gegerotewa/1/edit?js,console) works in JS. – VLAZ Mar 17 '21 at 15:58
  • 1
    Yeah, now after the fix it works the same in typescript, it just tells you that `title, ` is useless – Aleksey L. Mar 17 '21 at 16:03
  • ...there is the same bug in JS but since you don't use a linter, you've not noticed it. [What does a comma do in JavaScript expressions?](https://stackoverflow.com/q/3561043) | [Comma operator returns first value instead of second in argument list?](https://stackoverflow.com/q/5580596). `(title, people)` should be `people` – VLAZ Mar 17 '21 at 16:03
  • @AlekseyL. but I need the `title` otherwise it wont return the same array – Álvaro Mar 17 '21 at 16:05
  • What are you trying to do with `return Array.from(mapper, ([title, people]) => (title, people))` ? – Aleksey L. Mar 17 '21 at 16:05
  • BTW, If you click run in TS playground it prints exactly the same result – Aleksey L. Mar 17 '21 at 16:06
  • I am trying to do what I do on the JS snippet – Álvaro Mar 17 '21 at 16:06
  • 2
    @Álvaro *You are not returning the title!* The code `(title, people)` will *do nothing* with `title` and then return `people`. Since you say the JS version "works", obviously you don't need `title`, as it's not used. – VLAZ Mar 17 '21 at 16:06
  • I see `return Array.from(mapper, ([_,people]) => (people))` this works – Álvaro Mar 17 '21 at 16:10
  • 2
    You can simplify to `return Array.from(mapper.values())` then – Aleksey L. Mar 17 '21 at 16:23

0 Answers0