[英]JavaScript: how to check if elements in array are the correct length
我有一个名为“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,并且正数值 Agenda 应相同,但长度不超过 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
}
它就像匹配给定行的所有条件一样简单,为了清楚起见,您可以将各个检查分解为单独的函数。 您只需按长度检查字符串和从字符串中解析的数字,然后进行比较:
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.