why array.map returns undefined
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.filter(element => { if (element.age === 30) { return element; } }).map(({lastname})=>lastname) console.log(result1); output : ['kumar','kumar','kumar']
Here is what the above code is Doing:
1. First, we are filtering the array based on the age.
2. Then, we are mapping the lastname of the filtered array.