Home > Net >  Mapping Array of Objects Returns an Undefined Instead of Actual Objects
Mapping Array of Objects Returns an Undefined Instead of Actual Objects

Time:05-26

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": "[email protected]"
                },
              
            }
        
    },
    {
        "NG": 
            {
                "listed": "2022-05-26",
                "address": "road 1, Lagos",
                "distance": "12.3 km",
                "contact": {
                    "email": "[email protected]"
                },
              
            }
        
    },
   
];


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: '[email protected]' }
//     }
//   ]


// Instead of 

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

CodePudding user response:

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

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

CodePudding user response:

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": "[email protected]"
                },
            }
    },
    {
        "NG": 
            {
                "listed": "2022-05-26",
                "address": "road 1, Lagos",
                "distance": "12.3 km",
                "contact": {
                    "email": "[email protected]"
                },
            }
    },
   
];

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

  • Related