0

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

1
  • 1
    (x.US, x.NG) is just the comma operator. Commented May 25, 2022 at 21:31

2 Answers 2

1

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

data.map(x => (x.US ?? x.NG));
Sign up to request clarification or add additional context in comments.

1 Comment

Eventually you will want to make this more dynamic (assuming key names are not always known).
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.