Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法访问嵌套对象数组中的数据_Javascript_Arrays_Object - Fatal编程技术网

Javascript 无法访问嵌套对象数组中的数据

Javascript 无法访问嵌套对象数组中的数据,javascript,arrays,object,Javascript,Arrays,Object,我有一个对象数组,我想迭代并创建一个新的对象数组 首先映射数据,然后循环遍历每个对象以提取值。我想存储每个对象的位置名称和值 我的代码返回空结果。我无法更改数据的声明方式。有人能帮我理解为什么我总是得到空结果吗 [ { "euValue": null, "asValue": null } ] const数据=[{ 地点:[{ 地点:{ 名称:“欧洲” }, 价值:“爱尔兰” }, { 地点:{ 名称:“亚洲” }, 价值观:“中国” } ] }]; 常量formatD

我有一个对象数组,我想迭代并创建一个新的对象数组

首先映射数据,然后循环遍历每个对象以提取值。我想存储每个对象的位置名称和值

我的代码返回空结果。我无法更改数据的声明方式。有人能帮我理解为什么我总是得到空结果吗

[
  {
    "euValue": null,
    "asValue": null
  }
]
const数据=[{
地点:[{
地点:{
名称:“欧洲”
},
价值:“爱尔兰”
},
{
地点:{
名称:“亚洲”
},
价值观:“中国”
}
]
}];
常量formatData=()=>{
设formattedData=[];
让euValue,asValue;
formattedData=data.map(位置=>{
用于(位置中的常数l){
if(位置hasOwnProperty(l)){
const_this=位置[l];
euValue=\u this.Location==“欧洲”?\u this.Value:null;
asValue=\u this.Location==“亚洲”?\u this.Value:null;
}
}
返回{
尤维,
asValue
};
});
返回格式化数据;
};
const newData=formatData();

console.log(newData)我不知道您希望从代码中获得什么,但此代码可能会对您有所帮助

const数据=[{
地点:[{
地点:{
名称:“欧洲”
},
价值:“爱尔兰”
},
{
地点:{
名称:“亚洲”
},
价值观:“中国”
}
]
}];
常量formatData=()=>{
设formattedData=[];
formattedData=data.map(位置=>{
设euValue=[],
asValue=[];
用于(位置中的常数。位置){
if(位置。位置。hasOwnProperty(l)){
const_this=location.Locations[l];
如果(_this.Location.Name==“欧洲”)
euValue.push(_这个值);
else if(_this.Location.Name==“Asia”)
asValue.push(_这个值);
}
}
返回{
尤维,
asValue
};
});
返回格式化数据;
};
const newData=formatData();

console.log(newData)我不知道您希望从代码中获得什么,但此代码可能会对您有所帮助

const数据=[{
地点:[{
地点:{
名称:“欧洲”
},
价值:“爱尔兰”
},
{
地点:{
名称:“亚洲”
},
价值观:“中国”
}
]
}];
常量formatData=()=>{
设formattedData=[];
formattedData=data.map(位置=>{
设euValue=[],
asValue=[];
用于(位置中的常数。位置){
if(位置。位置。hasOwnProperty(l)){
const_this=location.Locations[l];
如果(_this.Location.Name==“欧洲”)
euValue.push(_这个值);
else if(_this.Location.Name==“Asia”)
asValue.push(_这个值);
}
}
返回{
尤维,
asValue
};
});
返回格式化数据;
};
const newData=formatData();

console.log(newData)
您丢失了第二个循环,也覆盖了
usValue
euValue
,在这种情况下,您最好使用
forEach
而不是
map

const数据=[{
地点:[{
地点:{
名称:“欧洲”
},
价值:“爱尔兰”
},
{
地点:{
名称:“亚洲”
},
价值观:“中国”
}
]
}];
常量格式数据=(数据)=>{
设formattedData=[],
值={};
data.forEach(位置=>{
用于(位置中的常数l){
if(位置hasOwnProperty(l)){
const_this=位置[l];
_this.forEach(el=>{
如果(el.Location.Name==“欧洲”){
值[“euValue”]=el.Value | | null
}
如果(el.Location.Name==“亚洲”){
值[“asValue”]=el.Value | | null
}
})
}
}
});
formattedData.push(值)
返回格式化数据;
};

log(formatData(data))
您丢失了第二个循环,同时覆盖了
usValue
euValue
,在这种情况下,您最好使用
forEach
而不是
map

const数据=[{
地点:[{
地点:{
名称:“欧洲”
},
价值:“爱尔兰”
},
{
地点:{
名称:“亚洲”
},
价值观:“中国”
}
]
}];
常量格式数据=(数据)=>{
设formattedData=[],
值={};
data.forEach(位置=>{
用于(位置中的常数l){
if(位置hasOwnProperty(l)){
const_this=位置[l];
_this.forEach(el=>{
如果(el.Location.Name==“欧洲”){
值[“euValue”]=el.Value | | null
}
如果(el.Location.Name==“亚洲”){
值[“asValue”]=el.Value | | null
}
})
}
}
});
formattedData.push(值)
返回格式化数据;
};

console.log(formatData(data))
假设在
数据
中,您可以有多个具有
位置
数组的对象,而该数组只有两个对象(一个用于欧洲,另一个用于亚洲),您应该将函数更改为类似这样的内容

const数据=[
{
地点:[
{
地点:{Name:“Europe”},
价值:“爱尔兰”
},
{
地点:{名称:“亚洲”},
价值观:“中国”
}
]
}
];
常量formatData=()=>{
//迭代所有数据对象
返回数据.map((topLocation)=>{
常数res={};
//循环定位子对象
topLocation.Locations.forEach((位置)=>{
const{Name}=location.location;
//根据Location.name决定保存值的位置
如果(名称==“欧洲”){
res.euValue=location.Value;
}如果(名称==“亚洲”){
res.asValue=location.Value;
}
});
返回res;
});
};
const newData=格式