📜  为什么 array.map 返回 undefined - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:02.780000             🧑  作者: Mango

代码示例2
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']



// same output but simple code for above snippet
let result1 = test.filter(element => 
              { 
                 if (element.age === 30) 
                 {
                    return element;
                 }
              }).map(({lastname})=>lastname)
console.log(result1)
output : ['kumar','kumar','kumar']