Convert JSON to CSV

    0

    0

    Giovanny Gongora

    Codiga's TypeScript Recipes

    Converts an array of objects to a comma-separated values (CSV) string that contains only the columns specified.

    const JSONtoCSV = (arr: Array<object>, columns: string[], delimiter = ',') =>
      [
        columns.join(delimiter),
        ...arr.map(obj =>
          columns.reduce(
            (acc, key) =>
              `${acc}${!acc.length ? '' : delimiter}"${!obj[key] ? '' : obj[key]}"`,
            ''
          )
        ),
      ].join('\n');
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.