This should be a really simple script that changes the title of a column between 3 columns. I got it to work once, but now it's coming up with an error I don't understand:
ReferenceError: "Today" is not defined. (line 10, file "Code")
Code is as follows:
function Progressions() {
var workoutSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Workouts");
var Increment = workoutSheet.getRange(2, 1, 1, 3).getValues();
var newRoutine =[[]]
if (Increment == [[Today,,]]){
var newRoutine = [[ ,Today, ]]
Increment.setValues(newRoutine);
} else {
if (Increment == [[,Today,]]){
var newToday = [[,,Today]]
Increment.setValues(newRoutine);
} else {
if (Increment == [[,,Today]]){
var newToday = [[Today,,]]
Increment.setValues(newRoutine);
}
}
}
Obviously Today is not defined as that's just the content of the array.
I think this script should just change the content of the newToday array and then enter it into the todayIncrement range.
Any ideas how I can remove this error and achieve my goal?
There are several reasons why your code isn't working such as "Today" but also in your arrays with you call .getValues(); the returned array will be something like [["","Today",""]]
You can't use Increment.setValues(------); as increment is an array an not a range object.
The if statement should be constructed
if(-----){
} else if (-----) {
} else {
}
This code does as you require:
function Progressions() {
var workoutSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Workouts");
var range = workoutSheet.getRange(2, 1, 1, 3);
var increment = range.getValues();
var newRoutine;
var testValue = "Today";
if (increment[0][0] == testValue) {
newRoutine = [["", "Today", ""]];
} else if (increment[0][1] == testValue) {
newRoutine = [["", "", "Today"]];
} else if (increment[0][2] == testValue) {
newRoutine = [["Today", "", ""]];
}
range.setValues(newRoutine);
}
An alternate implementation of the if-else chain that is easier to extend (e.g. if you had 365 different workouts as columns, the above method needs 365 if-else checks), at the cost of needing more JavaScript knowledge to understand how it works:
function Progressions() {
const workoutSheet = SpreadsheetApp.getActive().getSheetByName("Workouts"),
// 1 column per workout (here we have 3).
headerRange = workoutSheet.getRange(2, 1, 1, 3),
// Assume headers are in the range's first row.
headers = headerRange.getValues()[0],
newRoutine = [];
// Use Array#indexOf to do all the nasty "if-else if" checking.
const searchValue = "Today";
const columnIndex = headers.indexOf(searchValue);
if (columnIndex === -1)
throw new Error("'" + searchValue + "' not found in headers ('" + headerRange.getA1Notation() + "').");
// Set default header values (no Array#fill in Apps Script yet).
headers.forEach(function (s) { newRoutine.push(""); });
// Use addition and the modulus operator to increment and wrap.
const newIndex = (columnIndex + 1) % newRoutine.length;
newRoutine[newIndex] = searchValue;
// Write the new header values.
headerRange.setValues( [newRoutine] );
}
Related
I have some data in google sheet which I want to filter based on a certain criteria and return a corresponding value from another column. Lastly, count the number of elements in the returned column. Here is a sample data:
Sample data
A
B
1
Initials
Application Reference
2
MWB.KBB
1001
3
JET,JJB
1002
4
KBB
100,310,041,005
5
MKGC
1006
6
KBB
1007
Let's say I want to filter the data by searching for "KBB". I want to get all cells that contain the word "KBB" which should be three (3) cells. However, I am only getting two in return. The 1st row that contain two elements in a single cell is not included but it should be included. Lastly, count the elements in the returned column based on the criteria.
Here's the code I have tried:
function filter(){
//opened ss via url
const ws = ss.getSheetByName("Sample");
const range = ws.getRange(2,1,ws.getLastRow() - 1,2).getValues();
const initial = range.map(function(n){return n[0];});
const filtered = initial.filter(filterLogic);
Logger.log(initial); // [MWP, KBB, JET, JJB, KBB, MKGC, KBB]
Logger.log(filtered); // [KBB, KBB]
}
function filterLogic(name){
if(name == "KBB"){
return true;
} else {
return false;
}
}
The above code is only for the criteria. Not included is the counting of elements for the returned value from another column after the filter is applied.
What should I do so I can include the first row that contains the text "KBB" as well in my filtered data. Is there any other way around this?
Code:
function searchForKBB(n = "KBB") {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const osh = ss.getSheetByName("Sheet1");
let o = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).createTextFinder(n).matchEntireCell(false).findAll().map(rg => [rg.getA1Notation()]);
o.unshift(["Ranges"]);
osh.getRange(1,1,o.length,o[0].length).setValues(o)
}
Data:
A
B
1
Initials
Application Reference
2
MWB.KBB
1001
3
JET,JJB
1002
4
KBB
100,310,041,005
5
MKGC
1006
6
KBB
1007
Results:
Ranges
A2
A4
A6
Maybe you can do this:
```
function getAllKBBs(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ss1 = ss.getSheetByName("YOUR_SHEET_NAME");
var range = ss1.getRange(1,1,ss1.getLastRow(),4).getValues();
output = whenTextContains("KBB", range, 1, 1);
Logger.log(output.length);
} ```
where whenTextContains() function is in this repository
https://github.com/NikolaPlusEqual/GoogleAppsScriptFilters/blob/main/Functions
Or, you can copy this into you code and call above function:
function letterToColumn(letter){
var column = 0, length = letter.length;
for (var i = 0; i < length; i++)
{
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return column;
}
////// source for letterToColumn() function :
////// https://stackoverflow.com/questions/21229180/convert-column-index-into-corresponding-column-letter
var setData = {}
function whenTextContains(txt, rng, col, targetCol = 0){
if (typeof col == "number" && typeof txt == "string"){
setData.col = col;
setData.txt = txt;
}
else{
return;
}
var output = rng.filter(wtc);
if(targetCol == 0){
return output;
}
else if(typeof targetCol == "number"){
var result = output.map(function (item) {
return item[targetCol-1];
});
return result;
}
else if(typeof targetCol == "string"){
var targetnum = letterToColumn(targetCol);
var result = output.map(function (item) {
return item[targetnum-1];
});
return result;
}
else{
return;
}
}
function wtc(ar){
var txt = setData.txt;
var col = setData.col - 1;
var str = ar[col].toString();
return str.includes(txt);
}
I have created a Google script that pushes data every hour from the Capital Bikeshare API to a Google Sheet, but I have noticed that the way I am currently pulling the data doesn't maintain consistency over time. Here's the code I'm using:
function myFunction() {
// Set the active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var currentData = ss.getSheetByName("Current");
var historicData = ss.getSheetByName("Historic");
// Fetch API
var stationInfo = UrlFetchApp.fetch('https://gbfs.capitalbikeshare.com/gbfs/en/station_information.json');
var stationStatus = UrlFetchApp.fetch('https://gbfs.capitalbikeshare.com/gbfs/en/station_status.json');
// Get the current date and time
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
// Parse the JSON reply
var jsonInfo = stationInfo.getContentText();
var dataInfo = JSON.parse(jsonInfo);
var jsonStatus = stationStatus.getContentText();
var dataStatus = JSON.parse(jsonStatus);
// Create the data frame for every BID station
var stationInfo72 = dataInfo["data"]["stations"][69];
var stationStatus72 = dataStatus["data"]["stations"][69];
var stationInfo87 = dataInfo["data"]["stations"][83];
var stationStatus87 = dataStatus["data"]["stations"][83];
var stationInfo330 = dataInfo["data"]["stations"][311];
var stationStatus330 = dataStatus["data"]["stations"][311];
var stationInfo153 = dataInfo["data"]["stations"][143];
var stationStatus153 = dataStatus["data"]["stations"][143];
var stationInfo226 = dataInfo["data"]["stations"][213];
var stationStatus226 = dataStatus["data"]["stations"][213];
var stationInfo365 = dataInfo["data"]["stations"][342];
var stationStatus365 = dataStatus["data"]["stations"][342];
var stationInfo473 = dataInfo["data"]["stations"][446];
var stationStatus473 = dataStatus["data"]["stations"][446];
var outputStationsInfo = [stationInfo72, stationInfo87, stationInfo330, stationInfo153, stationInfo226, stationInfo365, stationInfo473]
var outputStationsStatus = [stationStatus72, stationStatus87, stationStatus330, stationStatus153, stationStatus226, stationStatus365, stationStatus473]
Logger.log(outputStationsInfo, outputStationsStatus)
// Create lists of each element
var outputHead = [];
var outputTail = [];
outputStationsInfo.forEach(function(elem,i) {
outputHead.push([elem["station_id"],elem["name"],elem["capacity"], elem["lat"], elem["lon"]]);
});
outputStationsStatus.forEach(function(elem,i) {
outputTail.push([elem["num_bikes_available"], elem["num_ebikes_available"], dateTime]);
});
// Publish arrays in the Current sheet
currentData.getRange(2,1,7,5).setValues(outputHead);
currentData.getRange(2,6,7,3).setValues(outputTail);
// Publish arrays in the Historic sheet
historicData.getRange(historicData.getLastRow() + 1,1,7,5).setValues(outputHead);
historicData.getRange(historicData.getLastRow() - 6,6,7,3).setValues(outputTail);
}
Essentially, I am drilling into the 69th item in the indexes of the JSONs to get the data that I need from two different APIs, and then I merge them together to create a data frame of everything I need to push to the sheet. However, sometimes the API does not report them in the normal order and I end up getting bikeshare stations that aren't in my study area. For example, 99% of the time the 69th item in the array is station_id = 72, but occasionally it's station_id = 73 or something.
Is there a way to conditionally pull a specific array based on the station_id number within the array? I feel like the answer might allow me to do a loop as well to clean this up. Any advice is helpful, as I'm super new to this.
You have to check if the element's station_id is as expected. If not, check through the surrounding parts of the array using a custom iterator.
Snippet:
/**
* #return indexes of the surrounding ``i`` in batches of 5
*/
function* checkSurroundings(i, lastIndex) {
let j = i;
function* check(ct, border, reverse = true, limit = border < 5 ? border : 5) {
const margin = reverse ? ct - limit : ct + limit;
while (ct - margin !== 0) yield reverse ? --ct : ++ct;
return ct;
}
while (i !== 0 || j < lastIndex) {
if (i !== 0) i = yield* check(i, i);
if (j < lastIndex) j = yield* check(j, lastIndex - j, false);
//console.log({ i, j });
}
}
var stations = dataInfo["data"]["stations"];
var stationInfo72 = stations[69];
const iter = checkSurroundings(69,stations.length-1)
//if station_id is not 72, loop through the surrounding indexes
while(stationInfo72["station_id"] !== 72){
const next = iter.next();
if(next.done) {
console.error("station id 72 not found");
break;
}
stationInfo72 = stations[next.value]
}
Snippet showing how checkSurroundings iterates:
/**
* #return indexes of the surrounding ``i`` in batches of 5
*/
function* checkSurroundings(i, lastIndex) {
let j = i;
function* check(ct, border, reverse = true, limit = border < 5 ? border : 5) {
const margin = reverse ? ct - limit : ct + limit;
while (ct - margin !== 0) yield reverse ? --ct : ++ct;
return ct;
}
while (i !== 0 || j < lastIndex) {
if (i !== 0) i = yield* check(i, i);
if (j < lastIndex) j = yield* check(j, lastIndex - j, false);
console.log({ i, j });
}
}
console.log("Order of iteration",[...checkSurroundings(50, 100)])
Conditionally picking elements: filter
For conditionally picking elements from an array in JavaScript, Array.prototype.filter should always be a consideration.
Create a predicate function that matches the shape of your data and checks for certain station IDs.
Here is a function that returns a predicate function. You put in the IDs you want in an array, and it returns the required function for filter.
function byStationId(stationIds) {
return function (obj) {
return stationIds.indexOf(obj.station_id) > -1;
};
}
var myStationFilter = byStationId([72, 73, 74]);
var outputStationsInfo = dataInfo.data.stations.filter(myStationFilter);
Transforming data: map
The pattern
var newArray = [];
oldArray.forEach(function (item) {
newArray.push(/* something based on item */);
});
can usually be replaced with Array.prototype.map
var newArray = oldArray.map(function (item) { return /* something based on item */});
Think of this as the "adapter" from one data shape to another.
function cleanInfo(info) {
return [info.station_id, info.name, info.capacity, info.lat, info.lon];
}
var outputHead = outputStationsInfo.map(cleanInfo);
For the dateTime injection, just do the same trick demonstrated above with the station IDs: have a function that takes a date string and returns the appropriate adapter function.
(Also note the provided date formatting utility Apps Scripts provides, Utilities.formatDate())
var dateTime = Utilities.formatDate(
new Date(),
ss.getSpreadsheetTimeZone(),
"yyyy-MM-dd HH:mm:ss"
);
function cleanStatus(dateTime) {
return function (status) {
return [status.num_bikes_available, status.num_bikes_available, dateTime];
};
}
var outputTail = outputStationsStatus.map(cleanStatus(dateTime));
Here's everything together, untested, just for inspiration. You must at the very least update the line with the station IDs to match your desired station IDs. Note that the helper functions for map and filter are at the bottom, taking advantage of JavaScript's hoisting feature.
function myFunction() {
// Set the active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get the current date and time
var dateTime = Utilities.formatDate(
new Date(),
ss.getSpreadsheetTimeZone(),
"yyyy-MM-dd HH:mm:ss"
);
// Fetch API
var stationInfo = UrlFetchApp.fetch(
"https://gbfs.capitalbikeshare.com/gbfs/en/station_information.json"
);
var stationStatus = UrlFetchApp.fetch(
"https://gbfs.capitalbikeshare.com/gbfs/en/station_status.json"
);
// Parse the JSON reply
var dataInfo = JSON.parse(stationInfo.getContentText());
var dataStatus = JSON.parse(stationStatus.getContentText());
// Create the data frame for every BID station
var myStationFilter = byStationId([72, 73, 74]); //!! UPDATE THESE NUMBERS
var outputStationsInfo = dataInfo.data.stations.filter(myStationFilter);
var outputStationsStatus = dataStatus.data.station.filter(myStationFilter);
// Create lists of each element
var outputHead = outputStationsInfo.map(cleanInfo);
var outputTail = outputStationsStatus.map(cleanStatus(dateTime));
// Publish arrays in the Current sheet
var currentData = ss.getSheetByName("Current");
currentData.getRange(2, 1, 7, 5).setValues(outputHead);
currentData.getRange(2, 6, 7, 3).setValues(outputTail);
// Publish arrays in the Historic sheet
var historicData = ss.getSheetByName("Historic");
historicData
.getRange(historicData.getLastRow() + 1, 1, 7, 5)
.setValues(outputHead);
historicData
.getRange(historicData.getLastRow() - 6, 6, 7, 3)
.setValues(outputTail);
//-------- helper functions ------------
function byStationId(stationIds) {
return function (obj) {
return stationIds.indexOf(obj.station_id) > -1;
};
}
function cleanInfo(info) {
return [info.station_id, info.name, info.capacity, info.lat, info.lon];
}
function cleanStatus(dateTime) {
return function (status) {
return [status.num_bikes_available, status.num_bikes_available, dateTime];
};
}
}
I am writing a script to copy and paste a range from one sheet to another. The pasted range size should be reduced by using two functions : one to delete rows with specific values and the other is an aggregate function.
I started getting this error after I introduced the aggregate function The function is basically reducing the array size using the reduce JS function.
I have replicated my problem here and the code is accessible in the script editor.
When I run the script I am getting the following error :
Incorrect range height was 28 but should be 7 (line 36, file "test")
I have no idea why am I getting this error. My aggregate function returns a properly formatted array with the right length.
function append_range(){
var origin_sheet = SpreadsheetApp.openById('1-2ZheMz1p01qwtwY3ghbNjJedYfGXeylnLEjDMCLpMw');//open the file
origin_sheet = origin_sheet.getSheetByName('test');
var rangeStart = 2;
var range = origin_sheet.getRange('A'+ (rangeStart.toString())+':T'+ (origin_sheet.getLastRow()).toString());
var dataFromRange = range.getValues();
var dataFromRangeLength = dataFromRange.length;
var destination_sheet = SpreadsheetApp.openById('1-2ZheMz1p01qwtwY3ghbNjJedYfGXeylnLEjDMCLpMw');
destination_sheet = destination_sheet.getSheetByName('append');
var rowLast = destination_sheet.getLastRow()+1;
Logger.log("row last" + rowLast);
var formattedRange = deleteRows(dataFromRange);
var groups = aggregate(formattedRange);
var aggregates = [];
for(var group in groups)
{
aggregates.push(groups[group]);
}
Logger.log(aggregates);
var formattedRangeLength = aggregates.length;
Logger.log("formattedRangeLength" + formattedRangeLength);
destination_sheet.getRange(rowLast,1,formattedRangeLength, 20).setValues(deleteRows(dataFromRange));
function isDate(sDate) {
if (isValidDate(sDate)) {
sDate = Utilities.formatDate(new Date(sDate), "PST", "yyyy-MM-dd");
}
return sDate;
}
function isValidDate(d) {
if ( Object.prototype.toString.call(d) !== "[object Date]" )
return false;
return !isNaN(d.getTime());
}
//
function deleteRows(dataRange){//just pass the range in an array and this method will return another array with filtered range
var formatted = dataRange.filter(function(e) {
return e[8]||e[9]||e[10]||e[11]||e[12]||e[13]||e[14]||e[15]||e[16]||e[17]||e[18]||e[19];
});
return formatted;
}
function aggregate(data)
{
var groups = data.reduce(
function(accumulator, previous){
{
var key = previous[1] + previous[3] + previous[5] + previous[6];
var group = accumulator[key];
if(group == null || typeof group == 'undefined')
{
accumulator[key] = previous;
}
else {
var startIndex = 8;
for(var i = startIndex; i < previous.length;i++)
{
group[i] += previous[i];
}
}
return accumulator;
}},
{});
return groups;
}
}
The .setValues() is not setting your aggregates array it is trying to set deleteRows(dataFromRange)
// Change the setValues() to your reduced array
destination_sheet.getRange(rowLast,1,formattedRangeLength, 20).setValues(aggregates);
I think this might work:
var output=deleteRows(dataFromRange));
destination_sheet.getRange(rowLast,1,output.length, output[0].length).setValues(deleteRows(output));
This assumes a non jagged array.
I am attempting to modify a google-apps-script to create an event in google calendar to follow up on emails with certain labels. That side of it I will take care of. The issue I'm having is getting TypeError: Cannot call method "push" of undefined when attempting to run the script. I am not seeing the mistake here so any help would be greatly appreciated.
function ReadEmails() {
var thread, subject, body, from,
date, emails, index, event = [], i;
var mySheet = SpreadsheetApp.getActiveSpreadsheet();
var LABEL = mySheet.getRange("D5").getValue();
var TOTAL = mySheet.getRange("D7").getValue();
emails = GmailApp.search("label:" + LABEL);
var count = emails.length;
if (count == 0)
return;
if (count > TOTAL)
index = getIndex(TOTAL, 0, count);
else {
for (i=0; i<count; i++)
index.push(i);
}
for (i=0; i<TOTAL; i++) {
var n = index[i];
if (emails[n]) {
thread = emails[n].getMessages()[0];
subject = thread.getSubject();
body = processHTML(thread.getBody(), 250);
link = thread.getId();
from = thread.getFrom();
date = Utilities.formatDate(thread.getDate(),
Session.getTimeZone(), "MMM dd, yyyy");
event = 'Follow up with' + from + 'on'+ subject + ', Next Friday
at 1PM';
CalendarApp.getDefaultCalendar().createEventFromDescription(event);
Logger.log('Event ID: ' + event.getId());
}
}
}
function getIndex(count, min, max) {
var results = [], index;
while ( count > 0) {
randNumber = Math.round(min + Math.random() * (max - min));
if (results.indexOf(randNumber) == -1) {
results.push(randNumber);
count--;
}
}
return results;
As #Pointy pointed out, the value of index is undefined. Actually, all of your variables thread, subject, body, from, date, emails, index, event are undefined at first, since you're assigning two values to them, which I have never seen in any language reference.
Later, you're re-assigning all of them except index, but the initial assignment of [], i is syntactically wrong - hence the undefined.
Just declare and initialize index like this: var index = []; and you should be fine.
The other variables you could initialize to null to make clear that you're gonna assign some proper values later on.
I am having difficulty pushing objects to an array of arrays via indices. Find below my current (awfully duplicative code) which reads through a CSV file's lines (Format: Date, reasonCode), and then creates FROM and TO (date) pairs depending on the reasonCode. This array is then used for Highcharts (Gantt chart). Please note the fromto1 and fromto2 arrays.
csv = csv.split(/\n/g);
var fromto1 = []; //array of FROM and TO pairs of code 1
fromto2 = []; //array of FROM and TO pairs of code 2
count = [];
lastFrom = [];
for (var i=1;i<3;i++) { //set all count and lastFrom variables to 0 //bs
count[i] = 0;
lastFrom[i] = 0;
}
jQuery.each(csv, function(i, line) {
line = line.split(','); //splits line, returns array of splitted values
date = parseInt(line[0], 10)*1000; //read date from line into string
reasonC = parseInt(line[2], 10); //read reasonC from line into string
if (reasonC == "1") {
count[1]++;
if (count[1] % 2 !=0){ //if it is an uneven value (FROM values)
lastFrom[1] = date; //temporary save the date in lastFrom[]
}
else { //if it is an even value (TO value), push the pair
fromto2.push({
from: lastFrom[1],
to: date
});
}
}
if (reasonC == "2") {
count[2]++;
if (count[2] % 2 !=0){
lastFrom[2] = date;
}
else {
fromto3.push({
from: lastFrom[2],
to: date
});
}
}
Why can't I replace the above code with this (Please note the fromto array of arrays):
csv = csv.split(/\n/g);
var fromto = [];
count = [];
lastFrom = [];
for (var i=1;i<3;i++) { //set all count and lastFrom variables to 0
count[i] = 0;
lastFrom[i] = 0;
fromto.push(new Array());
console.log(i+': New Array Pushed');
}
jQuery.each(csv, function(i, line) {
line = line.split(','); //splits line, returns array of splitted values
date = parseInt(line[0], 10)*1000; //read date from line into string
reasonC = parseInt(line[2], 10); //read reasonC from line into string
for (var c=1;c<3;c++) {
if (reasonC == c.toString()) {
count[c]++;
if (count[c] % 2 !=0){ //if it is an uneven value (FROM values)
lastFrom[c] = date; //temporary save the date in lastFrom[]
}
else { //if it is an even value (TO value), push the pair
fromto[c].push({
from: lastFrom[c],
to: date
});
}
}
}
}
I believe the problem is with fromto[c].push({ as it stays blank arrays.
I'm still a Jsnoob and couldn't find any answers on other threads, your help would be highly appreciated
There's quite a few things in your JavaScript that can do with some tips on how to do what you want to do using best practices, so an answer + advice.
1) multiple variables in a single declaration are separated by commas, not semi-colos:
var csv = csv.split(/\n/g),
fromto = [],
count = [],
lastFrom = [];
2) Don't use the Array object for making arrays.
fromto.push([]);
3) JS has function scoping only if you use var. Without var, variables are global.
jQuery.each(csv, function(i, line) {
line = line.split(',');
var date = parseInt(line[0], 10)*1000,
reasonC = parseInt(line[2], 10);
4) == is a coercing equality, and will see if there is any way two values can be considered the same. 4 == "4" is true, 4 === "4" is not.
if (reasonC == c) { ...
or
if (reasonC === c.toString()) { ...
5) JavaScript has forEach baked in, why would you use jQuery for something that's part of JavaScript already?
csv.forEach(function(line) {
...
});
And then the answer to your question, of course. It looks like you're trying to turn this data:
123,somevalue,1
456,somevalue,2
...
into this structure:
[
{ from: 123, to: 456 },
{ from: ..., to: ...},
...
]
You're relying on the line order to tell you which date is a from and which is a to; that's bound to go wrong, but you know your data best. (If I were writing this, I would operate on the assumption that line ordering is unknown)
var cvs = data.split(",");
parity = 0,
fields,
odd = [],
even = [],
fromTo = [];
cvs.forEach(function(line) {
parity = (parity + 1) % 2;
fields = line.split(",");
bin = parseInt(fields[2],10);
if(parity===1) {
odd[bin] = fields;
} else {
even[bin] = fields;
fromTo[bin].push({
from: odd[bin][0],
to: even[bin][0]
});
}
});
Now, this should work but this code is also really scary, because it still relies on line ordering in the CVS file, and without a hard guarantee that this is the case (like preprocessing validation), this (like your code) will do horrendously wrong things the moment two lines are accidentally in the wrong order.