Home > OS >  How to push key as iterative value in js?
How to push key as iterative value in js?

Time:01-29

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.

CodePudding user response:

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);

        }
        
    } 

CodePudding user response:

Please change the following line to this.

:from

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

: to

tarr.push({[headers[j]]:data[j]})

CodePudding user response:

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.

  •  Tags:  
  • Related