Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
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' }
//     }
//   ]

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

data.map(x => (x.US ?? x.NG));
about 4 years ago · Juan Pablo Isaza Report

0

Once you are iterating an object (and no matter in what language or with what function are you doing it) the value would be different each time. so on your case, when i is 0 you have "US", and when i is 1 you have "NG". If I understand correctly you want to do something like:

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"
                },
            }
    },
   
];

const x = Object.entries(data).map(([key, val])=>val);

console.log(x);

This one would allow you iterating through keys and values, no matter what the value is, you'd be able to have "ILS" or any other key as well

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!