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

141
Visualizações
Array of arrays into one single string with \n

I'm attempting to create one single string from an array of arrays, but with one condition, the last 2 elements (doesn't apply to first row)from the array need to be in "".
The array looks like this:

const data = [
    //( not applied to this row)
    [
        "id",
        "group_name",
        "admin",
        "email_",
        "og.id",
        "created_by.email",
        "students",
        "namesids"
    ],
    //( applied to the following rows)
    [
        "80",
        "Test group",
        "nilton.updated",
        "nilton@crowdform.co.uk",
        "22",
        "nilton@nilton.com",
        "nilton@nilton.com,pedro@com.com",
        "13,14,27"
    ],
    [
        "18",
        "PRO Group",
        "",
        "",
        "22",
        "sys@stys.com",
        "",
        "13000"
    ],
    [
        "10",
        "Test Group",
        "TEst",
        "Test@test.com",
        "22",
        "test.test@test.com",
        "",
        ""
    ]
]

Details:
The data that doesn't contain nothing need to be in quotes too. What I expect, and what I attempted until now:
expected (this is one string, last char from each line is \n):

const this_is_a_string = "
id,group_name,admin,email_,og.id,created_by.email,students,namesids
80,Test group,nilton.updated,nilton@crowdform.co.uk,22,nilton@nilton.com,"nilton@nilton.com,pedro@com.com","13,14,27"
10,Test Group,TEst,Test@test.com,22,test.test@test.com,"",""
...
"

What I attempted, didn't the the right answer:

data.map(objects => {
    const newAr = objects.reduce((arr, values, i) => {        
        arr = arr + ',' + values
        if (i === 6 || i ===7) return arr = arr + ',' + '"' + values + '"';
        return arr;
    }, "")
    console.log(newAr.substring(1));
    return;
})

If possible to create a function that is able to receive the data as first param and an array of position that need to be in quotes. The arrays will always have the same size as the first one, that why I know the positions that need to be in quotes.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For each row, use Array.prototype.slice() to separate the last two elements from the rest.

Then it's a matter of mapping those last two entries to quoted values and joining everything back up, separated by commas.

Each row can then be joined into a single string separated by newlines.

const data = [["id","group_name","admin","email_","og.id","created_by.email","students","namesids"],["80","Test group","nilton.updated","nilton@crowdform.co.uk","22","nilton@nilton.com","nilton@nilton.com,pedro@com.com","13,14,27"],["18","PRO Group","","","22","sys@stys.com","","13000"],["10","Test Group","TEst","Test@test.com","22","test.test@test.com","",""]];

const rowMapper = (row) =>
  [...row.slice(0, -2), ...row.slice(-2).map((v) => `"${v}"`)].join(",");

const [header, ...rows] = data;

const csv = [header.join(","), ...rows.map(rowMapper)].join("\n");

console.log(csv);


As mentioned in the comments, you might as well quote every value, even the headers and escape any embedded quotes

const csv = data
  .map((row) => row.map((v) => `"${v.replace(/"/g, '""')}"`).join(","))
  .join("\n");
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