Skip to content Skip to sidebar Skip to footer

Javascript Map, Reduce Not Working When Implemented Within Object Method

Based on the answer from this question I implemented the map reduce code within an object method. this.displayValueGraph = async () => { let scaleData = []; this.positions.f

Solution 1:

The root cause of the problem appears to have been the first forEach loop, where I included an await. I replaced the forEach with for in and it solved the problem.

this.displayValueGraph = async () => {
  let scaleData = [];
  for (const i in this.positions) {
    const pos = this.positions[i];
    scaleData[i] = [];
    let gdata = await pos.graphData;
    gdata.option.forEach((d) => {
      scaleData[i].push(d.map((x) => x * pos.size));
    });
  }
  let out;
  if (scaleData.length == 1) {
    out = scaleData[0];
  } else {
    out = scaleData.reduce((a, b) => b.map((x, j) => x.map((v, k) => a[j][k] + v)));
  }
};

Post a Comment for "Javascript Map, Reduce Not Working When Implemented Within Object Method"