【发布时间】: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