简体   繁体   English

JavaScript:如何检查数组中的元素长度是否正确

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

I have an array named "Items", with the contents of:我有一个名为“Items”的数组,其内容为:

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 和 Agenda 是否具有有效值。

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.项目的长度应至少为 1,不大于 3,并且正数值 Agenda 应相同,但长度不超过 5 位。

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.我还没有完成很多代码,只有我从excel文件中读取数据的部分,然后将其分配给范围变量。

$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. Kamen 我喜欢你的解决方案,我想让它工作,特别是现在你向我展示了如何从有效和无效行中获取所有值。

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.在此数组中,只有第 3 行是有效行。

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.然后我有2个数组。

How can I print the invalid and invalid values on the page and keeping the original order?如何在页面上打印无效和无效值并保持原始顺序?

Thank you kindly.非常感谢你。

EDIT #2编辑#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
}

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM