Reputation: 43
I am trying to read CSV data from a textarea and then depending on the conditions-if they pass or not-push them into two different arrays. I am struggling and I understand why my code inserts the data multiple times(because of my for loops, though I dont know how to fix that)but I do not why it inserts them in both success and failed Arrays. Here is what I have tried so far-must be pure javascript-and excuse my naming which is not great atm:
var addValue = document.getElementById('example').value;
filter = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
num = /0{2,}|1{2,}|2{2,}|3{2,}|4{2,}|5{2,}/,
cur = new Date(),
abbr = ["PA", "CT", "NJ", "MA", "IL", "ID", "OR"],
successArray = [];
failedArray = [];
if (addValue != "") {
var CSVvalue = addValue.split(',');
information.push(CSVvalue);
for (var i = 0; i <= information.length - 1; i++) {
for (var j = 0; j <= abbr.length - 1; j++) {
var email = information[i][3],
birthdate = new Date(information[i][2]),
zipCode = information[i][5],
diff = cur - birthdate,
age = Math.floor(diff / 31536000000),
state = information[i][4];
if ((filter.test(email)) && (!num.test(zipCode)) && (age > 21) && ((state != abbr[j]))) {
var lastSuccessArray = [];
successArray.push(information);
lastSuccessArray.push(successArray.slice(0));
} else {
var lastFailArray = [];
failedArray.push(information);
lastFailArray.push(failedArray.slice(0));
}
}
}
} else {
alert("You must enter a value");
}
Upvotes: 3
Views: 68
Reputation: 68393
just before pushing to success array check
if ( failedArray.indexOf( information[i] ) == -1 )
{
successArray.push(information[i]);
}
and vice versa for failed array
if ( successArray.indexOf( information[i] ) == -1 )
{
failedArray.push(information[i]);
}
your code can obviously be optimized but for your we will need to know more details about your array and logic.
Upvotes: 2
Reputation: 1668
var addValue = document.getElementById('example').value;
filter = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
num = /0{2,}|1{2,}|2{2,}|3{2,}|4{2,}|5{2,}/,
cur = new Date(),
abbr = ["PA", "CT", "NJ", "MA", "IL", "ID", "OR"],
successArray = [];
failedArray = [];
var information = [];//use array information in here
if (addValue != "") {
var CSVvalue = addValue.split(',');
information.push(CSVvalue);
for (var i = 0; i <= information.length - 1; i++) {
//no need of forloop 'j' using in here
var email = information[i][3],
birthdate = new Date(information[i][2]),
zipCode = information[i][5],
diff = cur - birthdate,
age = Math.floor(diff / 31536000000),
state = information[i][4];
if ((filter.test(email)) && (!num.test(zipCode)) && (age > 21) && ((abbr.indexOf(state) == -1))) {
var lastSuccessArray = [];
successArray.push(information);
lastSuccessArray.push(successArray.slice(0));
} else {
var lastFailArray = [];
failedArray.push(information);
lastFailArray.push(failedArray.slice(0));
}
}
} else {
alert("You must enter a value");
}
Dont use forloop abbr
this will push values the length of abbr. so instead just use abbr.indexOf(state) == -1
.
Upvotes: 2