78892

Javascript map, reduce not working when implemented within object method

<h3>Question</h3>

Based on the answer from this question I implemented the map reduce code within an object method.

this.displayValueGraph = async () => { let scaleData = []; this.positions.forEach(async (pos, 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))); } };

The code by itself works fine. I have taken the input data (above scaleData) and run it through the map reduce function and the output is as expected. But if I include it as part of this method it does nothing. It doesn't throw any errors, it simply returns an empty array.

I have tried adding an empty array as an "initial value", but it doesn't help.


<h3>Answer1:</h3>

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))); } };

来源:https://stackoverflow.com/questions/62237704/javascript-map-reduce-not-working-when-implemented-within-object-method

Recommend

  • kotlin getting a subscriber to observe an observable using RxJava2
  • response time is higher, when I call procedure in oracle through simple Jdbc call compared to the lo
  • bind_rows() error: by reading in a function?
  • Using QThread and QTimer to run a methode
  • Is there a modern ( e.g. CLR ) replacement for bison / yacc?
  • overriding equals and hashcode methods in java?
  • search the database based on the value selected from database
  • Appending spaces with str_pad
  • Storyboard iOS MBProgressHUD
  • Advertising Identifier for devices lower than iOS 6.0
  • Use Perl to Add GIF Image Other Than 8-bit to PDF
  • Can't perform CORS request using Angularjs
  • What is need of Assign/Deassign in Verilog?
  • the IBM_JAVA error for running jobs in Hadoop 2.2.0
  • Get spring boot pagination number starts from 1 instead of 0
  • VS2010: Ctrl-PgUp / -PgDown like in browsers
  • Threads and Concurrent Modification Exception working with a list
  • How to get a time and Date Separately?
  • Admob in ListView not clickable
  • Unable to start a WebView from an AsyncTask
  • Java Collections.shuffle() weird behaviour [closed]
  • How to use Flask's render_template from an ajax POST form submit
  • time column in sqlite using gorm
  • xpath assertion failure with dynamic xpath
  • Google Spreadsheet Script to Blink a range of Cells
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • php “page caching” solution suggestions for CMS Applications
  • Jersey serializes character value to ASCII equivalent numeric string
  • WPF custom control and direct content support
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • No OpKernel was registered to support Op 'Conv2D' with these attrs
  • JavaScript RegExp Replace