1

Hello I have this code and It's working

  for (let i in list) {
    db.push({
      name: list[i],
      values: []
    });

    for (let j in data) {
      db[i].values.push({
        Date: data[j].Date,
        count: parseInt(data[j][list[i]])
      });
    };
    db[i].values.pop();
  };

Im trying to convert it to this

  list.forEach((el, i) => {
    db.push({
      name: list[i],
      values: Array.prototype.call(data.forEach((d, j) => {
        this.push({
          Date: data[j].Date,
          count: parseInt(data[j][list[i]])
        });
      }))
    })
  })
zEn feeLo
  • 1,877
  • 4
  • 25
  • 45
  • There is no `Array.prototype.call` method. – Bergi May 15 '20 at 19:06
  • Please add the initial values of `db`, `list` and `data` to your code snippet. From the current post it's not even clear whether they are objects or arrays. – Bergi May 15 '20 at 19:07
  • as I said the first part is working, Im trying to Improve my code , so Its clear they are Arrays – zEn feeLo May 15 '20 at 22:00
  • 1
    In that case, have a look at [why is using `for…in` on arrays such a bad idea?](https://stackoverflow.com/q/500504/1048572). – Bergi May 16 '20 at 12:56

1 Answers1

0

You could slice data to get only the items without the last and map the value.

let shortData = data.slice(0, -1),
    db = list.map(name => ({
        name,
        values: shortData.map(({ Date, [name]: count }) => ({
            Date,
            count: +count
        })
    });
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392