简体   繁体   English

包含一个元素的数组包含许多我想成为数组元素的对象

[英]Array containing one element containing a number of objects which I'd like to be elements of array

I have an array of one element containing several objects.我有一个包含多个对象的元素数组。 I don't know how to work with these objects.我不知道如何使用这些对象。 I'd like each object to be turned into an element of an array, so that I'll be able to refer to Array[0].Time to get "36:50" or Array[2].Seconds to get 2235.我希望每个 object 都变成数组的一个元素,这样我就可以引用 Array[0].Time 获取“36:50”或 Array[2].Seconds 获取 2235。

Link to Json: https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json Json链接: https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json

Although it's very easy to deal with objects I converted it for you in arrays. Use Array#map to iterate through your array over the objects and map every old entry to a new one.尽管处理对象非常容易,但我在 arrays 中为您转换了它。使用Array#map遍历对象上的数组,map 将每个旧条目迭代到一个新条目。
Create for each element a new empty array and add to this for every entry a new entry.为每个元素创建一个新的空数组,并为每个条目添加一个新条目。 Use for this Object.values to get the values.用于此Object.values以获取值。

Note: The sequence from the entries in the array I can't change without extra programming and it's mandatory that the objects allways have this structure (otherwise the output would change).注意:如果没有额外的编程,我无法更改数组中条目的顺序,并且对象始终具有此结构是强制性的(否则 output 会更改)。

 let arr = [ { "Time": "36:50", "Place": 1, "Seconds": 2210, "Name": "Marco Pantani", "Year": 1995, "Nationality": "ITA", "Doping": "Alleged drug use during 1995 due to high hematocrit levels", "URL": "https://en.wikipedia.org/wiki/Marco_Pantani#Alleged_drug_use" }, { "Time": "36:55", "Place": 2, "Seconds": 2215, "Name": "Marco Pantani", "Year": 1997, "Nationality": "ITA", "Doping": "Alleged drug use during 1997 due to high hermatocrit levels", "URL": "https://en.wikipedia.org/wiki/Marco_Pantani#Alleged_drug_use" }, { "Time": "37:15", "Place": 3, "Seconds": 2235, "Name": "Marco Pantani", "Year": 1994, "Nationality": "ITA", "Doping": "Alleged drug use during 1994 due to high hermatocrit levels", "URL": "https://en.wikipedia.org/wiki/Marco_Pantani#Alleged_drug_use" }, { "Time": "37:36", "Place": 4, "Seconds": 2256, "Name": "Lance Armstrong", "Year": 2004, "Nationality": "USA", "Doping": "2004 Tour de France title stripped by UCI in 2012", "URL": "https://en.wikipedia.org/wiki/History_of_Lance_Armstrong_doping_allegations" }]; let res = arr.map(obj => { let arr = []; Object.values(obj).forEach(val => arr.push(val)); return arr; }); console.log(res);

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

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