映射对象数组返回未定义而不是实际对象

奥卢瓦费米·阿金耶米

我有一个嵌套对象数组,我目前正在映射它以取回一个对象数组。但是我没有得到实际的对象,而是返回了一个未定义的部分。我不知道为什么 undefined 是我结果的一部分。我实际上需要知道为什么 Undefined 是我的结果的一部分。有人请告诉我我做错了什么。谢谢

我的代码如下

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

群聚

x.NG 在索引 0 中不存在。尝试...

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章