Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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.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']
Comment

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']
Comment

PREVIOUS NEXT
Code Example
Javascript :: https express 
Javascript :: js empty map 
Javascript :: variables in js 
Javascript :: array some 
Javascript :: accepting form data node js 
Javascript :: reverse individual words in a sentence javascript 
Javascript :: update data in json using javascript 
Javascript :: tinymce get plain text 
Javascript :: tailwind container class size 
Javascript :: nodejs fs writefile base64url 
Javascript :: does pycharm support javascript 
Javascript :: react 
Javascript :: pagination.js example codepen 
Javascript :: js get the filename you uploaded 
Javascript :: how to replace div element with another in javascript 
Javascript :: javascript ascii character 
Javascript :: js url pathname 
Javascript :: ion icon react 
Javascript :: wavesurf js 
Javascript :: regex match any character 
Javascript :: how to start node server 
Javascript :: get current tab from chrome extension developer 
Javascript :: js sum digits 
Javascript :: javascript close app phonegap 
Javascript :: knex.js migration create 
Javascript :: add parameter at the end of url from jquery with refreshing 
Javascript :: react loop through array 
Javascript :: monaco editor get value 
Javascript :: random trong js 
Javascript :: use css child selector inside js 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =