0

I have an array of objects like this:

const rates = [{
  price: 20,
  time_periods: [
    {
      period: "1 Week",
      rate: 5,
    },
    {
      period: "2 Months",
      rate: 9,
    },
  ],
}, {...}]

It will then be converted into a table with all cells editable. I have a callback that tells me which cell was exactly edited. It returns 3 values: a value from input, number of row (array index in our case), and name of the column (key of the object) I save it to my array like that: rates[index][key] = value

The problem is that I can get values like this: 20, 2, time_periods[0].rate, which causes the above line doesn't work. It works with eval like this: eval(`rates[${index}].${key}`) but I heard that using eval is not the best idea.

Is there any other way to deal with this? I could try to change the structure of my array to be less complicated but yeah, wanted to know if I can achieve my goal without doing it.

  • 1
    Read this answer, especially the added note: [Convert JavaScript string in dot notation into an object reference](https://stackoverflow.com/a/6394168/13762301) – pilchard Mar 29 '21 at 14:29
  • @pilchard Yeah, that's why I said that I can also change the structure of this array. I'm getting this from an API so it's not my idea that it looks like that. I managed to fit it into the app but if it causes problems like this I will just edit it before doing something with it – Patryk Grzyb Mar 29 '21 at 14:48

0 Answers0