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
How to push key as iterative value in js?

Hi i have a two for loop contains checking the each loop length and push into key value pair of array

 const reader = new FileReader(); // filereader
  reader.readAsText(e.target.files[0]); // read as text
  reader.onload = () => {
      const allText = reader.result;
    var allTextLines = allText.split(/\r\n|\n/);
    var headers = allTextLines[0].split('\t');
    var lines = [];

    for (var i=1; i<allTextLines.length; i++) {
        var data = allTextLines[i].split('\t');
        if (data.length == headers.length) {

            var tarr = [];
            for (var j=0; j<headers.length; j++) {

              
                tarr.push(headers[j]+":"+data[j]);
            }
            
             lines.push(tarr);

        }
        
    } 

and my output is getting below

[Array(2),Array(2)]

output now i'm getting

     [
      [
       0:'product:PA123'
       1:'value:123'
       2:'price:$234'
       ],
       [
       0:'product:PA234'
       1:'value:235'
       2:'price:$24'
       ],
    
      ]
    
    
    expected output
     [
       [
        product:PA123,
        value :123,
        price :234
       ],
       [
        product:PA123,
        value :123,
        price :234
        ]
    
      ]

i need to push headers[j] as key but i can't.How to do that in JS ? I'm literally new . please bare me.THanks

apologize for bad formatting.

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

0

Edited: You already getting your expected result. just dont get confuse with index keys 0, 1, 2. Dev console shows index of array as key to make it more readable. still you are confuse just console stringified result.

console.log(JSON.stringify(lines));

For output as array of model: i.e.

[
   {
      product:PA123,
      value :123,
      price :234
   },
   {
      product:PA123,
      value :123,
      price :234
    }    
 ]
// code
for (var i=0; i<allTextLines.length; i++) {
    var data = allTextLines[i];
    if (data.length == headers.length) {

        var model= {};
        for (var j=0; j<headers.length; j++) 
        {
          model[headers[j]] = data[j];
        }    
        lines.push(model);    
    }    
}
console.log(JSON.stringify(lines)); 

For output as array of string array: i.e.

[
   [
      "product:PA123",
      "value :123",
      "price :234"
   ],
   [
      "product:PA123",
      "value :123",
      "price :234"
    }    
 ]

 //code
 for (var i=0; i<allTextLines.length; i++) {
    var data = allTextLines[i];
    if (data.length == headers.length) {

        var stringArray= [];
        for (var j=0; j<headers.length; j++) 
        {
          stringArray.push(headers[j]+":"+data[j]);
        }    
        lines.push(stringArray);    
    }    
}
console.log(JSON.stringify(lines));

Hope this will answer your query.

about 4 years ago · Juan Pablo Isaza Relatório

0

for (var i=0; i<allTextLines.length; i++) {
        var data = allTextLines[i];
        if (data.length == headers.length) {

            var tarr = [];
            for (var j=0; j<headers.length; j++) 
            {
               let a = headers[j];
               let b = {}  
               b[headers[j]] = data[j];
               tarr.push(b)****
            }
            
             lines.push(tarr);

        }
        
    } 
about 4 years ago · Juan Pablo Isaza Relatório

0

Please change the following line to this.

:from

tarr.push(headers[j]+":"+data[j])

: to

tarr.push({[headers[j]]:data[j]})
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