erasmo carlos
erasmo carlos

Reputation: 682

JavaScript: how to check if elements in array are the correct length

I have an array named "Items", with the contents of:

0: {ID: "50425", Item: "1", Agenda: "61163", Title: "Comment"}
1: {ID: "50425", Item: "2", Agenda: "62394", Title: "Program"}
2: {ID: "50425", Item: "3", Agenda: "57122", Title: "Action"}
3: {ID: "50425", Item: "4", Agenda: "56234", Title: "Future"}
4: {ID: "50425", Item: "5", Agenda: "33325", Title: "Report"}
5: {ID: "50425", Item: "6", Agenda: "33326", Title: "Rights"}
6: {ID: "50425", Item: "7", Agenda: "33327", Title: "Division"}
7: {ID: "50425", Item: "8", Agenda: "41726", Title: "Award"}

I need to learn how to check if Item and Agenda have valid values.

Item should be of length of at least 1, no greater than 3, and a positive numeric value Agenda should be the same, but no greater than 5 digits long.

I have looked up information on how to check if an array has data in it, but I have not learned how to check is the values in the array are valid.

Is there any examples someone can point me to, or willing to share how to achieve this that would be great.

I do not have much code done yet, only the part that I read the data from the excel file, and then assign it to a scope variable.

$scope.ProcessExcel = function (data) {
     //Read the Excel File data.
     var workbook = XLSX.read(data, {
         type: 'binary'
     });
     //Fetch the name of First Sheet.
     var firstSheet = workbook.SheetNames[0];
     //Read all rows from First Sheet into an JSON array.
     excelRows = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[firstSheet]);
     excelRows.forEach(function (element) {
         element.MeetingID = $scope.meetingId;
     });
     //Display the data from Excel file in Table.
     $scope.$apply(function () {
         
         $scope.Items = excelRows;
         $scope.IsVisible = true;
         $scope.disableSubmit = false;            
     });
     console.log(excelRows);
 };

Any help is greatly appreciated.

Thank you, Erasmo

EDIT

Kamen I like your solution and I want to make it work especially now that you showed me how to get all the values from the valid and invalid rows.

One thing I wonder if you have a suggestion:

My array:

let excelRows2 = [
    { ID: "50425", Item: "4441", Agenda: "6", Title: "Comment" },
    { ID: "50425", Item: "-2", Agenda: "694", Title: "Program" },
    { ID: "50425", Item: "344", Agenda: "522", Title: "Action" },
    { ID: "50425", Item: "444", Agenda: "-1", Title: "Future" },
    { ID: "50425", Item: "544", Agenda: "adfas", Title: "Report" },
    { ID: "50425", Item: "6778", Agenda: "36", Title: "Rights" },
    { ID: "50425", Item: "7bbbbb", Agenda: "327", Title: "Division" },
    { ID: "50425", Item: "-1", Agenda: "4726", Title: "Award" }
];

In this array, only the 3rd row is a valid row.

When I print the values in the manner you showed me:

let validRows = excelRows2.filter((row) => rowIsValid(row));
let invalidRows = excelRows2.filter((row) => !rowIsValid(row));

console.log(validRows);
console.log(invalidRows);

Then I have 2 arrays.

How can I print the invalid and invalid values on the page and keeping the original order?

Thank you kindly.

EDIT #2

I think I came up with a solution:

let excelRows2 = [
    { ID: "50425", AgendaItem: "4441", LegistarID: "6", Title: "Comment" },
    { ID: "50425", AgendaItem: "-2", LegistarID: "694", Title: "Program" },
    { ID: "50425", AgendaItem: "344", LegistarID: "522", Title: "Action" },
    { ID: "50425", AgendaItem: "444", LegistarID: "-1", Title: "Future" },
    { ID: "50425", AgendaItem: "544", LegistarID: "adfas", Title: "Report" },
    { ID: "50425", AgendaItem: "6778", LegistarID: "36", Title: "Rights" },
    { ID: "50425", AgendaItem: "7bbbbb", LegistarID: "327", Title: "Division" },
    { ID: "50425", AgendaItem: "-1", LegistarID: "4726", Title: "Award" }
];

excelRows2.forEach(function (row) {
    var response = rowIsValid(row);
    if (!response) {
        row.IsValid = false;
    }
    else {
        row.IsValid = true;
    }
});

function rowIsValid(row) {
    return (
        (fitsLength(row.AgendaItem, 1, 3) && isPositive(row.AgendaItem)) &&
        (fitsLength(row.LegistarID, 1, 5) && isPositive(row.LegistarID))
    );
}

function fitsLength(str, min, max) {
    return str.length >= min && str.length <= max;
}

function isPositive(strNum) {
    return parseInt(strNum, 10) > 0
}

Upvotes: 0

Views: 73

Answers (1)

Kamen Minkov
Kamen Minkov

Reputation: 3722

It's as simple as all your conditions matching for a given row, and you can break the individual checks into separate functions for clarity. The strings you check simply by length and the numbers you parse from the strings and then do a comparison:

let excelRows = [
    { ID: "50425", Item: "1", Agenda: "61163", Title: "Comment" },
    { ID: "50425", Item: "2", Agenda: "62394", Title: "Program" },
    { ID: "50425", Item: "3", Agenda: "57122", Title: "Action" },
    { ID: "50425", Item: "4", Agenda: "56234", Title: "Future" },
    { ID: "50425", Item: "5", Agenda: "33325", Title: "Report" },
    { ID: "50425", Item: "6", Agenda: "33326", Title: "Rights" },
    { ID: "50425", Item: "7", Agenda: "33327", Title: "Division" },
    { ID: "50425", Item: "8", Agenda: "41726", Title: "Award" }
]; // dummy assignment, in the original example that would be the result of XLSX.utils.sheet_to_row_object_array(...)

function rowIsValid(row) {
    return (
        (fitsLength(row.Item, 1, 3) && isPositive(row.Item)) &&
        (fitsLength(row.Agenda, 1, 5) && isPositive(row.Agenda))
    );
}

function fitsLength(str, min, max) {
    return str.length >= min && str.length <= max;
}

function isPositive(strNum) {
    return parseInt(strNum, 10) > 0
}

excelRows.map((row) => console.log(rowIsValid(row)));

Edit: to filter the valid and invalid rows, you can simply do the following:

let validRows = excelRows.filter((row) => rowIsValid(row));
let invalidRows = excelRows.filter((row) => !rowIsValid(row));

Upvotes: 2

Related Questions