admin管理员组文章数量:1290984
Hello I want to display a message if a filtered array length === 0
users.filter((user) => {
return user.name.includes('john')
}).map((user, index) => {
console.log(user)
})
i´m not sure where to put it. could it be inside the map? what´s the correct way?
Hello I want to display a message if a filtered array length === 0
users.filter((user) => {
return user.name.includes('john')
}).map((user, index) => {
console.log(user)
})
i´m not sure where to put it. could it be inside the map? what´s the correct way?
Share Improve this question edited Jul 23, 2019 at 22:43 handsome asked Jul 23, 2019 at 22:31 handsomehandsome 2,42211 gold badges54 silver badges81 bronze badges 3-
Why not
const johns = users.filter(...); if (!johns.length) { /* display the message */}
or if dont need the filtered array you could useconst johnExists = users.some(,,,)
– Yury Tarabanko Commented Jul 23, 2019 at 22:39 -
The code you've provided contains syntax errors.
(user.name.includes('john')
is a bracket mismatch. – Olian04 Commented Jul 23, 2019 at 22:40 - corrected. thanks @Olian04 – handsome Commented Jul 23, 2019 at 22:43
5 Answers
Reset to default 4You can assign this method to a variable which will be a filtered array. And then check the length of that.
Let filteredArrayLength = users.filter((user) => {
return (user.name.includes('john')
}). length;
After this you can put a conditional statement with value check of variable,
if(filteredArrayLength === 0){
console.log("print something");
}
You can use .map only if you want to perform some operation on each of the filtered user object. Like if you want to fetch full name of the user, you can do a string concatenation of first and last name using map which will return a object with full name, it will return a array of same length with modified values. Check this for example: https://www.w3schools./jsref/jsref_map.asp
If filter()
returns empty array the map()
will just not run and will pass on the empty array also so use it on the result of the bination
const res = users.filter((user) => {
return user.name.includes('john')
}).map((user, index) => {
console.log(user)
// need to return something or this map is pointless
})
console.log(res.length)
if the requirement is just know if their is any user name 'john', It could be done with ES6 some
method;
if(!users.some(user=>user.name.includes('john'))){
console.log('User John does not exist');
}
Here is my attempt
let users = [/*some array*/];
users = users.filter( user => user.name == 'john');
if( user.length > 0 ){
//run code if the length is more than 0
users = users.map((user, index) => { console.log(user) });
}
under map is not the correct place becuase you are iterating through your filtered data.
you should store the filter resultset as follows:
let users = users.filter((user) => {
return (user.name.includes('john'))
});
if(users.length > 0){
//rest of the logic or iterate
}else{
// print or display your message
}
本文标签: javascriptfiltered array lengthStack Overflow
版权声明:本文标题:javascript - filtered array length - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741520902a2383178.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论