[FIXED] Wie repliziert man die Pandas explode() Funktionalität mit Javascript?

Ausgabe

Ich automatisiere meine Google Sheets in App-Skripten. Ich bin auf ein Szenario gestoßen, in dem eine Zelle mehrere Werte hat. Ich möchte explodees wie in Python.

Beispiel Ein- und Ausgabe:

input = [["Hi",12,"One line
two line
three line", "God"],["Bye",13,"check","man"]]
output = [["Hi",12,"One line","God"],["Hi",12,"two line","God"],["Hi",12,"Three line","God"],["Bye",13,"check","man"]]

Nachfolgend mein Teilversuch

var final = y.map(r => {
    return r.map(x => {
      if(x.toString().indexOf("/n") > -1)
      {
        x = x.toString().split("/n");
      }
      return x;      
      });
  });

Lösung

Verwenden Sie reducezum Schleifen: splitdie Zeichenfolge durch neue Zeile \n, mapdas geteilte Array und verbinden Sie es mit der übergeordneten Zeile und dem vorherigen Akkumulator und flat.

/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/ 

const input = [["Hi",12,"One line\ntwo line\nthree line", "God"],["Bye",13,"check","man"]];
const output = input.reduce((acc,[a,b,c],i) =>  [acc,c.split("\n").map(sc => [a,b,sc])].flat() ,[]);
console.log(output);
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>


Beantwortet von –
TheMaster


Antwort geprüft von –
Mildred Charles (FixError Admin)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like