我有一个嵌套对象数组,我目前正在映射它以取回一个对象数组。但是我没有得到实际的对象,而是返回了一个未定义的部分。我不知道为什么 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] 删除。
我来说两句