0

So, I have a problem with a spread operator which is really annoying me right now. I searched a lot and nothing helped me.

When I used console.log(...val) it shows data perfectly without any error, but when I try const data = ...val it throws an error 'Expression expected'

{stats &&
            stats.map(val => {
              const title = Object.keys(val)[0]
              const values = Object.values(val)
              console.log('*************', ...values)

              return (
                <div className="stats__stats--chart">
                  <div className="chart-name">
                    <h3>{title}</h3>
                  </div>
                  <div className="chart-data">
                    <DataChart data={values} />
                  </div>
                </div>
              )
            })}

I am using React and Typescript and I know this should work since I've done it before. const values is a 2D array, inner arrays hold objects and I want to extract all arrays from that array so I can use it with that chart, but spread operator is broken somehow. Is there some kind of config to fix that? Whats the problem with that

VLAZ
  • 18,437
  • 8
  • 35
  • 54
Dario K
  • 119
  • 9
  • 2
    [There is no spread operator!](https://stackoverflow.com/a/64612639/) So, it's bit "broken" as much as "it can never work, nor has it ever been intended to work". If you need to [Merge/flatten an array of arrays](https://stackoverflow.com/q/10865025) then you need `data = val.flat()` but I'm not even sure if that's what you're after as `console.log(val)` will still show you a bunch of arrays which is definitely *not* what you'd get from `.flat()` – VLAZ Apr 15 '21 at 09:42
  • That is exactly what I needed, thanks. Can you post an answer so I can approve? And why does it work in console.log() then – Dario K Apr 15 '21 at 09:55

1 Answers1

0

I am not sure how you use the values, but if you use the data in the DataChart component you should use [...data[0], ...data[1]] as variable.