0

All, I have a module to import CSV file and fetch data and display in a grid. Fetched the data in array but I expected value must be in a particular JavaScript data structure.

Here my sample code

function processData(allText) {
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
console.log("headers-->"+headers)
var lines = [];

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

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

console.log("details ="+lines)

});

allText Value

serial,Asset Type,id 
Asset1,Equipemnt,id1
Asset2,Equipemnt,id2
Asset3,Equipemnt,id3
Asset4,Equipemnt,id4

My output:

Serial:Asset1,Asset Type:Equipment,id:RF0001,
Serial:Asset2,Asset Type:Equipment,id:R0002,
Serial:Asset3,Asset Type:Equipment,id:R0003,
Serial:Asset4,Asset Type:Equipment,id:F0004,
Serial:Asset5,Asset Type:Equipment,id:F0005,
Serial:Asset6,Asset Type:Equipment,id:0006,
Serial:Asset7,Asset Type:Equipment,id:007,

Expected structure:

 {
    serial:["Asset1","Asset1","Asset2","Asset3","Asset4"],
    Asset Type:["Equipment","Equipment","Equipment","Equipment","Equipment"],
    id:["id1","id2","id3","id4",]
    }

How to achieve this structure?

1
  • Can you share the allText you are using for your tests? Commented Nov 7, 2017 at 9:18

2 Answers 2

2
function processData(allText) {
   var allTextLines = allText.split(/\r\n|\n/);
   var headers = allTextLines[0].split(',');
   console.log("headers-->"+headers)
   //initializing resulting json
   var lines = {};

   //initializing arrays for all the headers
   for(var i=0; i<headers.length; i++){
               lines[headers[i]] = [];
   }


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

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

       }
   }
       return lines
   }




   //format all text is taken up (I tested on a string)
   allText = "serial,Asset Type,id\n Asset1,Equipemnt,id1\nAsset2,Equipemnt,id2\nAsset3,Equipemnt,id3\nAsset4,Equipemnt,id4"

Result : enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

didnt helped :(
what is response you are getting now?
[object object]
could you send me a sample of allTextLines?
what is the content in the object array?
|
2

You first line is header and wanted to set as a key object . So you already get that var headers = allTextLines[0].split(','); thus assign that object key by object[key] = value format .

I assume that your header and lines splited key has the same thus I loop allTextLines and used the same key for both headers and allTextLines !!

var all = "serial,Asset Type,id \n"+
"Asset1,Equipemnt,id1 \n"+
"Asset2,Equipemnt,id2 \n"+
"Asset3,Equipemnt,id3 \n"+
"Asset4,Equipemnt,id4";

processData(all);

function processData(allText) {
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
var lines = {};
//set headers as key
for(var i = 0 ; i < headers.length;i++) {
   lines[headers[i]] = [];
}
//assign relative value to key
for (var i=1; i<allTextLines.length; i++) {
    var data = allTextLines[i].split(',');
    for(var d in data) {
      lines[headers[d]].push(data[d]);
    }
}

console.log(lines)

}

2 Comments

I appreciate that
how to handle if row values contains any special char like "," in any of the record ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.