Reputation: 31
I'm currently trying to figure out how to parse a csv string into an object that will allow me to go to a customers name in the first object and then being able to access the said customers information in a sub array based off of which name.
csvString = "roll R coaster,0124454,20.21\n
I know that the .split() function will split the string based off a delimiter and place it in an array. If I use "\n" as my first delimiter, 5 array elements are created for each client followed by their respective information.
array[0] = "roll R coaster,0124454,20.21"
Upvotes: 3
Views: 54
Reputation: 10975
To achieve expected , you can use below option of using array map() method twice - one for FirstName,MiddleName, LastName and other for remaining comma separated values - TransactionNumber, TransactionAmount
var csv = "roll R coaster,0124454,20.21".split(",");
var keys = [["FirstName", "MiddleName", "LastName"], "TransactionNumber","TransactionAmount"]
var Obj ={}
csv.map((val, i)=>{
if(i == 0){
val.split(" ").map((v,idx)=>{
Obj[keys[0][idx]] = v.toUpperCase()
})
}else{
Obj[keys[i]] = val
}
})
console.log(Obj)
https://codepen.io/nagasai/pen/aqaGQN?editors=1010
Upvotes: 0
Reputation: 2454
I think this code does pretty much what you need.
csvString = "roll R coaster,0124454,20.21\n Sammy Smocks,000006,(20.20)\n maxwell BLANCO ,002125,(15),\n Will Monsters,003576,6.9,15\n Trank Burger,103529,9.56,5";
const objects = csvString.split('\n ')
.map(x => {
const [name, TransactionNumber, TrAm] = x.split(',');
TransactionAmount = TrAm[0] === '(' ? +TrAm.slice(1, -1) : +TrAm;
FullName = name
.replace(/^\s{2,}/g, ' ')
.replace(/\s{3}/g, ' ')
.replace(/\s{2}$/g, ' ');
const [FirstName, MiddleName, LastName] = FullName.split(' ');
return {
FirstName,
MiddleName,
LastName,
TransactionNumber,
TransactionAmount
}
});
console.log(objects);
Upvotes: 1
Reputation: 1245
So we take in the csv as a string, split it on the '\n' to get each row. We then split the row using regex, you suggest by your wanted object properties that this is a predictable input per-row so we take that and pop it into a json object and return it. You can then access it via myData[i]).firstName. Woith a minor change you could, if you wanted, give each row an identified such as the transaction number, so myData["000006"].lastName would return 'Smocks'.
let csvString = "roll R coaster,0124454,20.21\n Sammy Smocks,000006,(20.20)\n maxwell BLANCO ,002125,(15),\n Will Monsters,003576,6.9,15\n Trank Burger,103529,9.56,5";
let myData = [];
csvString.split('\n').forEach((row) => {
let rowData = row.match(/(\w+) *(\w*) *(\w*),(.+),(.+)/i);
myData.push({
firstname: rowData[1],
middleName: rowData[3] ? rowData[2] : "",
lastName: rowData[3] ? rowData[3] : rowData[2],
transactionNumber: rowData[4],
transactionAmount: rowData[5]
});
});
console.log(myData);
Output:
(5) [{…}, {…}, {…}, {…}, {…}]
0:{firstname: "roll", middleName: "R", lastName: "coaster", transactionNumber: "0124454", transactionAmount: "20.21"}
1:{firstname: "Sammy", middleName: "", lastName: "Smocks", transactionNumber: "000006", transactionAmount: "(20.20)"}
2:{firstname: "maxwell", middleName: "", lastName: "BLANCO", transactionNumber: "002125", transactionAmount: "(15),"}
3:{firstname: "Will", middleName: "", lastName: "Monsters", transactionNumber: "003576,6.9", transactionAmount: "15"}
4:{firstname: "Trank", middleName: "", lastName: "Burger", transactionNumber: "103529,9.56", transactionAmount: "5"}
Upvotes: 0