Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
How to download CSV from a nested object in javascript?

I want to iterate over an object in Javascript and create a CSV file from its internal objects.

const list = {
    "right": {
        "label": "Right",
        "items": ["Jack", "Dough"]
    },
    "wrong": {
        "label": "Wrong",
        "items": ["Rain", "Over"]
    },
    "nope": {
        "label": "Nope",
        "items": ["No", "Way"]
    }
};
const downLoadCsv = (list) => {
    let csvContent = '';
    Object.keys(statusM).forEach((obj) => {
      console.log(obj);
      //do something like obj.items and iterate over internal object - items
      //but typeof obj is string here
    });
    const anchorEle = document.createElement('a');
    anchorEle.href = `data:text/csv;charset=utf-8,${encodeURI(csvContent)}`;
    anchorEle.target = '_blank';
    anchorEle.download = `${key}.csv`;
    anchorEle.click();
  };

I tried to iterate over obj but I found that the typeof obj is string. I was rather expecting an object like as follows:-

    {
        "label": "Right",
        "items": ["Jack", "Dough"]
    },

I'm expecting an output like this:-

  Jack, Right
  Dough, Right
  Rain, Wrong
  Over, Wrong
  No, Nope
  Way, Nope

Can someone please help?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

  • so, you need to conver entire object to the next structure:
[
 ['Jack', 'Right'],
 ['Dough', 'Right'],
]
  • it accomplished via this: Object.values(list).map(i => i.items.map(j => [j, i.label])).flat()

  • after that i added extra method toCsv for formatting - to allow break line

const list = {
    "right": {
        "label": "Right",
        "items": ["Jack", "Dough"]
    },
    "wrong": {
        "label": "Wrong",
        "items": ["Rain", "Over"]
    },
    "nope": {
        "label": "Nope",
        "items": ["No", "Way"]
    }
};

const downLoadCsv = (list) => {
    let csvContent = toCsv(Object.values(list).map(i => i.items.map(j => [j, i.label])).flat());

    const anchorEle = document.createElement('a');
    anchorEle.href = `data:text/csv;charset=utf-8,${encodeURI(csvContent)}`;
    anchorEle.target = '_blank';
    anchorEle.download = `test.csv`;
    anchorEle.click();
  };
  
  function toCsv(arr){
    return arr.reduce(function(csvString, row){
        csvString += row.join(',') ;
    csvString += "\r\n";  //";";//"\n";
        return csvString;
    }, '');
}

downLoadCsv(list)

about 4 years ago · Juan Pablo Isaza Denunciar

0

here, you need to put values instead of key but here, I have hide unrelated cuz I am unknown about that code.

const list = {
    "right": {
        "label": "Right",
        "items": ["Jack", "Dough"]
    },
    "wrong": {
        "label": "Wrong",
        "items": ["Rain", "Over"]
    },
    "nope": {
        "label": "Nope",
        "items": ["No", "Way"]
    }
};
const downLoadCsv = (list) => {
    let csvContent = '';
    Object.values(list).forEach((obj) => {
      console.log(obj);
      //do something like obj.items and iterate over internal object - items
      //but typeof obj is string here
    });
    /*
    const anchorEle = document.createElement('a');
    anchorEle.href = `data:text/csv;charset=utf-8,${encodeURI(csvContent)}`;
    anchorEle.target = '_blank';
    anchorEle.download = `${key}.csv`;
    anchorEle.click();
    */
  };
  
  downLoadCsv(list)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda