Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
How to create downloadable csv file from a javascript object consisting of array?

I have an object like this:-

{
    "SUCCESS": [
        "apple",
        "orange",
        "grapes"
    ],
    "FAILED": ["kiwi"],
    "NOPE": ["peach"]
}

I want to have a CSV as follows:-

apple  SUCCESS
orange SUCCESS
grapes SUCCESS
kiwi   FAILED
peach  NOPE

I figured out this piece of code but unable to iterate over this:-

const downLoadCsv = (key, items) => {
    let csvContent = '';
    if (!items.length) {
      return;
    }
    items.forEach((item) => {
      csvContent += `${item},`;
      csvContent += '\n';
    });

    const anchorEle = document.createElement('a');
    anchorEle.href = `data:text/csv;charset=utf-8,${encodeURI(csvContent)}`;
    anchorEle.target = '_blank';
    anchorEle.download = `${key}.csv`;
    anchorEle.click();
  };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Well, the point here is that, as you wrote, items is not an array, but an object instead.

What you should do is to iterate both items keys and the list of items for each key, so:

const downLoadCsv = (key, items) => {
  let csvContent = '';

  Object.keys(items).forEach(status => {
    items[status].forEach(fruit => {
      csvContent += `${fruit},${status}\n`;
    })
  });

  if (csvContent !== '') {
    const anchorEle = document.createElement('a');
    anchorEle.href = `data:text/csv;charset=utf-8,${encodeURI(csvContent)}`;
    anchorEle.target = '_blank';
    anchorEle.download = `${key}.csv`;
    anchorEle.click();
  }
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda