【问题标题】:JavaScript: how to check if elements in array are the correct lengthJavaScript:如何检查数组中的元素是否是正确的长度
【发布时间】:2020-11-03 22:41:07
【问题描述】:

我有一个名为“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"}

我需要学习如何检查 Item 和 Agenda 是否具有有效值。

项目的长度应至少为 1,不大于 3,并且为正数值 议程应相同,但长度不得超过 5 位数。

我已经查找了有关如何检查数组中是否有数据的信息,但我还没有学会如何检查数组中的值是否有效。

有没有人可以指点我的例子,或者愿意分享如何实现这一点。

我还没有完成太多代码,只有我从 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);
 };

非常感谢任何帮助。

谢谢你, 伊拉斯莫

编辑

Kamen 我喜欢你的解决方案,我想让它发挥作用,特别是现在你向我展示了如何从有效和无效行中获取所有值。

有一件事我想知道您是否有建议:

我的数组:

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" }
];

在这个数组中,只有第 3 行是有效行。

当我按照您向我展示的方式打印值时:

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

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

然后我有 2 个数组。

如何在页面上打印无效值和无效值并保持原来的顺序?

谢谢。

编辑#2

我想我想出了一个解决方案:

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
}

【问题讨论】:

    标签: javascript arrays validation


    【解决方案1】:

    它就像您匹配给定行的所有条件一样简单,并且您可以将各个检查分解为单独的函数以清楚起见。您仅通过长度检查的字符串和您从字符串中解析的数字然后进行比较:

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

    编辑:要过滤有效和无效的行,您可以简单地执行以下操作:

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

    【讨论】:

    • 谢谢卡门,我会试试你的例子。我认为我需要使用循环来完成任务; .map 函数的目的是什么?我会尽量让你的例子适合我手头的任务。再次感谢您。
    • 我想我可以让你的代码示例工作。我只需要适应它以适应我的需要。我希望您不介意一个问题,是否易于显示,如何返回一条消息,告诉用户返回 false 的项目?非常感谢,
    • Array.map() 可以像 for 循环一样服务,但语法更简单 - 它获取数组中的所有条目并将它们中的每一个作为内部函数的第一个参数传递 - @987654321 @ - 所以 (row) =&gt; console.log(rowIsValid(row)) 将为数组中的每个条目调用一次。您可以使用它或 Array.filter() 过滤有效和无效条目,请参阅我编辑的原始答案。
    • 我可以看到无效值和有效值。我更新了我的问题。我想知道是否可以保持数组的原始顺序然后将其显示在页面上?否则,我认为我能做的就是拥有表格,只有在有无效值时才会显示的表格。也许您看到了一种更优雅的方式来做到这一点?
    • 乍一看,您可以将 for 循环中的内容简化为 row.isValid = rowIsValid(row),即删除 if-else 和额外变量。在我看来,这样会更具可读性。有时直接写入输入数据可能不是一个好主意,但您会自己决定。
    猜你喜欢
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多