Monday, October 24, 2022
HomeWordPress Developmentjavascript: Object to CSV - DEV Neighborhood 👩‍💻👨‍💻

javascript: Object to CSV – DEV Neighborhood 👩‍💻👨‍💻


It is a very a lot wanted requirement in all most all internet purposes as and when backend would not assist obtain for given outcomes.

We usually populate ends in desk -> To populate we have to iterate over array accommodates information objects.

Like,

const information = [
{ name: 'Tom', id: '1234'},
{ name: 'James', id: '3456'},
{ name: 'Jerry', id: '7890'},
{ name: 'Peter', id: '4321'}
];
Enter fullscreen mode

Exit fullscreen mode

And, the above types desk as:

Image description

Simply need similar outcomes to be downloaded as CSV.

  const headers = Object.keys(information[0]).be part of();
    const content material = information.map(worth => Object.values(worth).be part of());
    const csv = [headers].concat(content material).be part of("n");
    const component = doc.createElement('a');
    component.href = 'information:textual content/csv;charset=utf-8,' + encodeURI(csv);
    component.goal = '_blank';
    component.obtain = 'export.csv';
    component.click on();
Enter fullscreen mode

Exit fullscreen mode

Carried out. Your CSV will get obtain as present in under:

Image description

At all times welcome any enhancements.

Thanks.

Comply with: https://twitter.com/urstrulyvishwak



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments