Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mapping Array of Objects Returns an Undefined Instead of Actual Objects

I have an Array of Nested Objects which I’m presently mapping to get back an Array of Objects. But Instead of getting actual objects, Part of my expected result is returning an Undefined. I don’t know why undefined is part of my result. I actually need to know why Undefined is part of my result. Someone to please tell me what I’m doing wrong. Thanks

My Code below

const data = [
    {
        "US": 
            {
                "listed": "2022-05-25",
                "address": "Kingston road, New York",
                "distance": "37.3 km",
                "contact": {
                    "email": "abc@gmail.com"
                },
              
            }
        
    },
    {
        "NG": 
            {
                "listed": "2022-05-26",
                "address": "road 1, Lagos",
                "distance": "12.3 km",
                "contact": {
                    "email": "def@gmail.com"
                },
              
            }
        
    },
   
];


console.log(data.map((x, i)=>{
    return (
        x.US, x.NG   )
}))


// OutPut Here below 

// [
//     undefined,
//     {
//       listed: '2022-05-26',
//       address: 'road 1, Lagos',
//       distance: '12.3 km',
//       contact: { email: 'def@gmail.com' }
//     }
//   ]


// Instead of 

// [
//     {
//         "listed": "2022-05-25",
//         "address": "Kingston road, New York",
//         "distance": "37.3 km",
//         "contact": {
//             "email": "abc@gmail.com"
//         },
      
//     },
//     {
//       listed: '2022-05-26',
//       address: 'road 1, Lagos',
//       distance: '12.3 km',
//       contact: { email: 'def@gmail.com' }
//     }
//   ]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

x.NG does not exist in index 0.
Try…

data.map(x => (x.US ?? x.NG));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading