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 use const 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
Add a ment  | 

5 Answers 5

Reset to default 4

You 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