Why does javascript map function return undefined?

前端 未结 7 1272
梦谈多话
梦谈多话 2020-12-07 16:24

My code

 var arr = [\'a\',\'b\',1];
 var results = arr.map(function(item){
                if(typeof item ===\'string\'){return item;}  
               });
<         


        
相关标签:
7条回答
  • 2020-12-07 16:59

    You can implement like a below logic. Suppose you want an array of values.

    let test = [ {name:'test',lastname:'kumar',age:30},
                 {name:'test',lastname:'kumar',age:30},
                 {name:'test3',lastname:'kumar',age:47},
                 {name:'test',lastname:'kumar',age:28},
                 {name:'test4',lastname:'kumar',age:30},
                 {name:'test',lastname:'kumar',age:29}]
    
    let result1 = test.map(element => 
                  { 
                     if (element.age === 30) 
                     {
                        return element.lastname;
                     }
                  }).filter(notUndefined => notUndefined !== undefined);
    
    output : ['kumar','kumar','kumar']
    
    0 讨论(0)
提交回复
热议问题