[英]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.