'How mapping specific property from arraylist
I m looking a way to get all email property from my postList list to filtering max chars authorized, the problme is i don't know really how mapping email property from 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);
Solution 1:[1]
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);
Solution 2:[2]
Hello try to use filter method
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luca Fonta |
| Solution 2 | Abdelraouf Ferah |
