Žygimantas Augūnas
Žygimantas Augūnas

Reputation: 33

React: How do I make a two dimensional array from one dimensional array?

I've recently started learning React. Anyways,I want to insert data in scatter chart which requires one two dimensional variable named "data". However, I don't know how to create it because I already have another one dimensional array. Tried something like this:

copyToFinal() {
        const  data = [
            this.state.arrayvar.map((number, index) =>
            {x:index, y:number})
        ];
    }

It's a function that creates a variable from array (arrayvar). I need an array which would fit for the graph library or maybe You could suggest any other way to insert my array into the graph which requires x and y values:

<ScatterChart min={null} max={null} data={data} xtitle="Index" ytitle="Random Numbers" /

Upvotes: 1

Views: 1528

Answers (1)

Steve Holgado
Steve Holgado

Reputation: 12071

You are not actually returning an object from your mapping function.

Try this:

const data = [
  this.state.arrayvar.map((number, index) => {
    return { x: index, y: number }
  })
]

...or:

const data = [
  this.state.arrayvar.map((number, index) =>
    ({ x: index, y: number })
  )
]

Upvotes: 1

Related Questions