Importing CSV data into JavaScript to create customer objects with properties.
Copy link to clipboard
Copied
Hello, all,
I have written a script that I use in Illustrator that opens a window where I type in customer information and the script enters it into varios text frames on an Illustrator proof template that I made. I have a list of regular customers that I would like to have appear on a drop-down menu. Currently, I have this customer information inside the JavaScript code itself, but I want to be able to have the script import the data from a CSV file. I have successfully imported the data, but it comes in as a single string of text, separated with the new line character (/n). I am trying to make each customer a separate object, with the object's properties the correct element from the imported CSV file.
I guess the short version is how can I make each element of an array a value of an object's property, as well as having the number of customer objects depend on how many customers are in CSV file. Each customer gets their own row of data in Excel.
Explore related tutorials & articles
Copy link to clipboard
Copied
If all you want is to convert an array of strings to an array of objects:
var array1 = ["a,b,c", "a1,b1,c1", "a2,b2,c2", "a3,b3,c3"];
var array2 = [];
for (var i = 1; i < array1.length; i++) {
var temp1 = array1[0].split(",");
var temp2 = array1[i].split(",");
var o = {};
for (var j = 0; j < temp2.length; j++) {
o[temp1[j]] = temp2[j];
}
array2.push(o);
}
// array2 = [
// {
// a: "a1",
// b: "b1",
// c: "c1"
// },
// {
// a: "a2",
// b: "b2",
// c: "c2"
// },
// {
// a: "a3",
// b: "b3",
// c: "c3"
// }
// ]
Copy link to clipboard
Copied
Thanks, femkeblanco! I will give that a try and report back.
Copy link to clipboard
Copied
HI @josephb2381006, if you need to go further than @femkeblanco's suggestion, here is a nice CSV parser function (by Andy VanWagoner) that I found some time ago. I keep it on hand whenever I need to parse CSV.
/**
* Parse CSV text into 2-dimensional
* array of rows and columns.
* @author Andy VanWagoner
* @url https://stackoverflow.com/questions/1293147/example-javascript-code-to-parse-csv-data
* example reviver:
* function (r, c, v) { return v.replace(/[\r\n]/g, ''); };
* @param {String} csv - the raw comma-separated text
* @param {Function} [reviver] - function that may modify the parsed value (default: revive the raw value).
* @returns {Array<Array<Number>>} - [[cell,cell,cell],[cell,cell,cell]], access by [rowIndex] [columnIndex]
*/
function parseCSV(csv, reviver) {
reviver = reviver || function (r, c, v) { return v; };
var chars = csv.split(''), c = 0, cc = chars.length, start, end, table = [], row;
while (c < cc) {
table.push(row = []);
while (c < cc && '\r' !== chars[c] && '\n' !== chars[c]) {
start = end = c;
if ('"' === chars[c]) {
start = end = ++c;
while (c < cc) {
if ('"' === chars[c]) {
if ('"' !== chars[c + 1]) {
break;
}
else {
chars[++c] = ''; // unescape ""
}
}
end = ++c;
}
if ('"' === chars[c]) {
++c;
}
while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ',' !== chars[c]) {
++c;
}
} else {
while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ',' !== chars[c]) {
end = ++c;
}
}
row.push(reviver(table.length - 1, row.length, chars.slice(start, end).join('')));
if (',' === chars[c]) {
++c;
}
}
if (end === c - 1) {
// row.push(reviver(table.length - 1, row.length, ''));
}
if ('\r' === chars[c]) {
++c;
}
if ('\n' === chars[c]) {
++c;
}
}
return table;
};
You can pass a reviver argument. This is a function that can modify the value. Here's an example reviver that strips leading/trailing whitespace and linefeeds:
/**
* Removes LF, CR and leading/trailing whitespace
* @param {Number} r - row index
* @param {Number} c - column index
* @param {String} v - cell value
* @returns {String}
*/
function trimSpaceRemoveLineFeedsReviver(r, c, v) {
return v.replace(/(^\s*|\s*$|\r|\n)/g, '');
};
- Mark
Copy link to clipboard
Copied
Thanks, Mark! I am going to check this out.

