我正在尋找一種方法來從我的 postList 串列中獲取所有電子郵件屬性以過濾授權的最大字符數,問題是我真的不知道如何從 postList 映射電子郵件屬性...
const postList = [
{"postId": 1, "id": 1, "email": "[email protected]"},
{"postId": 1, "id": 2, "email": "[email protected]"},
{"postId": 1, "id": 3, "email": "[email protected]"},
{"postId": 1, "id": 4, "email": "[email protected]"},
]
const onlyEmailValid = []
for (let i = 0; i < postList.length; i ) {
if(postList.email.value.length > max_chars) {
console.log("email : " postList.email " has too long");
continue
}
onlyEmailValid.push(postList[i]);
}
console.log(clearJobList);
uj5u.com熱心網友回復:
你好嘗試使用filter
方法
const postList = [
{"postId": 1, "id": 1, "email": "[email protected]"},
{"postId": 1, "id": 2, "email": "[email protected]"},
{"postId": 1, "id": 3, "email": "[email protected]"},
{"postId": 1, "id": 4, "email": "[email protected]"},
]
const onlyEmailValid = []
const max_chars = 13
postList.filter(item=>{
(item.email.length > max_chars) ? console.log("email : " item.email " has too long") : onlyEmailValid.push(item);
})
console.log(onlyEmailValid);
uj5u.com熱心網友回復:
const postList = [
{"postId": 1, "id": 1, "email": "[email protected]"},
{"postId": 1, "id": 2, "email": "[email protected]"},
{"postId": 1, "id": 3, "email": "[email protected]"},
{"postId": 1, "id": 4, "email": "[email protected]"},
]
const onlyEmailValid = []
const max_chars = 15 //example
for (let i = 0; i < postList.length; i ) {
if(postList[i].email.length > max_chars) {
console.log("email : " postList[i].email " has too long");
continue
}
onlyEmailValid.push(postList[i]);
}
console.log(onlyEmailValid);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/453576.html
標籤:javascript 列表 增量
上一篇:多行字串的識別符號預期錯誤