简体   繁体   English

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

[英]Cant access data inside nested array of objects

I have an array of objects that I want to iterate over and create a new array of objects.我有一个对象数组,我想对其进行迭代并创建一个新的对象数组。

First I map over the data, then I loop through each object to extract the values.首先我 map 遍历数据,然后循环遍历每个 object 以提取值。 I want to store the Location name and value from each object.我想存储每个 object 的位置名称和值。

My code is returning null results.我的代码返回 null 结果。 I can't change the way data is declared.我无法更改声明数据的方式。 Can someone help me understand why I keep getting null results?有人可以帮我理解为什么我不断得到 null 结果吗?

[
  {
    "euValue": null,
    "asValue": null
  }
]

 const data = [{ Locations: [{ Location: { Name: "Europe" }, Value: "Ireland" }, { Location: { Name: "Asia" }, Value: "China" } ] }]; const formatData = () => { let formattedData = []; let euValue, asValue; formattedData = data.map(location => { for (const l in location) { if (location.hasOwnProperty(l)) { const _this = location[l]; euValue = _this.Location === "Europe"? _this.Value: null; asValue = _this.Location === "Asia"? _this.Value: null; } } return { euValue, asValue }; }); return formattedData; }; const newData = formatData(); console.log(newData);

Edit Expected result is编辑预期结果是

[
  {
    "euValue": “Ireland”,
    "asValue": “China”
  }
]

you missing a second loop also you overwriting the usValue and euValue and you better use forEach instead of map in this case.你错过了第二个循环,你也覆盖了usValueeuValue并且在这种情况下你最好使用forEach而不是map

 const data = [{ Locations: [{ Location: { Name: "Europe" }, Value: "Ireland" }, { Location: { Name: "Asia" }, Value: "China" } ] }]; const formatData = (data) => { let formattedData = [], values = {}; data.forEach(location => { for (const l in location) { if (location.hasOwnProperty(l)) { const _this = location[l]; _this.forEach(el => { if (el.Location.Name === "Europe") { values["euValue"] = el.Value || null } if (el.Location.Name === "Asia") { values["asValue"] = el.Value || null } }) } } }); formattedData.push(values) return formattedData; }; console.log(formatData(data))

Assuming that inside data you could have multiple objects with a Location array that have only 2 objects (one for Europe and another one for Asia) you should change your function to something like this假设内部data您可以有多个对象,其Location数组只有 2 个对象(一个用于欧洲,另一个用于亚洲),您应该将 function 更改为类似这样

 const data = [ { Locations: [ { Location: { Name: "Europe" }, Value: "Ireland" }, { Location: { Name: "Asia" }, Value: "China" } ] } ]; const formatData = () => { // iterate all data objects return data.map((topLocation) => { const res = {}; // loop over Location children objects topLocation.Locations.forEach((location) => { const { Name } = location.Location; // decide where to save Value base on the Location.name if (Name === "Europe") { res.euValue = location.Value; } else if (Name === "Asia") { res.asValue = location.Value; } }); return res; }); }; const newData = formatData(); console.log(newData);

I don't know what do you want to get from your code but this code may help you.我不知道你想从你的代码中得到什么,但这段代码可能会对你有所帮助。

 const data = [{ Locations: [{ Location: { Name: "Europe" }, Value: "Ireland" }, { Location: { Name: "Asia" }, Value: "China" } ] }]; const formatData = () => { let formattedData = []; formattedData = data.map(location => { let euValue = [], asValue = []; for (const l in location.Locations) { if (location.Locations.hasOwnProperty(l)) { const _this = location.Locations[l]; if (_this.Location.Name === "Europe") euValue.push(_this.Value); else if (_this.Location.Name === "Asia") asValue.push(_this.Value); } } return { euValue, asValue }; }); return formattedData; }; const newData = formatData(); console.log(newData);

Using Array.prototype.flatMap() might help you get the array you desire in a cleaner way:使用 Array.prototype.flatMap() 可能会帮助您以更简洁的方式获得所需的数组:

 const data = [{ Locations: [{ Location: { Name: "Europe" }, Value: "Ireland" }, { Location: { Name: "Asia" }, Value: "China" } ] }]; const formatData = () => { const formattedData = data.flatMap(item => { const object = {} item.Locations.map(location => { const continent = location.Location.Name let country = {} if (continent === 'Europe') country = { euValue: location.Value } if (continent === 'Asia') country = { asValue: location.Value } Object.assign(object, country) }); return object }); return formattedData; } const newData = formatData(); console.log(newData);

I'm sure many of the other answers are fine but the way I did it was to do the classic for loop to iterate over the data.我确信许多其他答案都很好,但我这样做的方式是使用经典的 for 循环来迭代数据。 I would have liked to have kept your ternary operators but I think you may need the if/else syntax.我本来希望保留您的三元运算符,但我认为您可能需要 if/else 语法。

 var data = [{ Locations: [{ Location: { Name: "Europe" }, Value: "Ireland" }, { Location: { Name: "Asia" }, Value: "China" } ] }]; const formatData = () => { let formattedData = []; let euValue, asValue; formattedData = data.map(location => { for (const l in location) { if (location.hasOwnProperty(l)) { const _this = location[l]; for (let i = 0; i < _this.length; i++) { if (_this[i].Location.Name === "Europe") { euValue = _this[i].Value; } else if (_this[i].Location.Name === "Asia") { asValue = _this[i].Value; } else { euValue, asValue = null; } } } } return { euValue, asValue }; }); return formattedData; }; const newData = formatData(); console.log(newData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM