[英]Printing values from a deeply nested object as an array using JavaScript
I have the following object and I am trying to create a function that prints the result as an array that contains only the nested 'rgba' values as an array:我有以下对象,我正在尝试创建一个将结果打印为数组的函数,该数组仅包含嵌套的“rgba”值作为数组:
const colorObject = {
"colors": [
{
"color": "black",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255, 255, 255, 1]
}
},
{
"color": "white",
"category": "value",
"code": {
"rgba": [0, 0, 0, 1]
}
},
{
"color": "red",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255, 0, 0, 1]
}
},
{
"color": "blue",
"category": "hue",
"type": "primary",
"code": {
"rgba": [0, 0, 255, 1]
}
},
{
"color": "yellow",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255, 255, 0, 1]
}
},
{
"color": "green",
"category": "hue",
"type": "secondary",
"code": {
"rgba": [0, 255, 0, 1]
}
},
]
}
So far, I have gone the long route and produced the result, but I know that there must be a more efficient way:到目前为止,我已经走了很长的路并产生了结果,但我知道必须有一种更有效的方法:
function dispArrayRgbaCode() {
let arrayRgbaCode = [
colorObject['colors'][0]['code']['rgba'],
colorObject['colors'][1]['code']['rgba'],
colorObject['colors'][2]['code']['rgba'],
colorObject['colors'][3]['code']['rgba'],
colorObject['colors'][4]['code']['rgba'],
colorObject['colors'][5]['code']['rgba'],
]
console.log(arrayRgbaCode)
}
dispArrayRgbaCode()
I am brand new and have checked out working with objects in the MDN docs.我是全新的,并且已经在 MDN 文档中检查过使用对象。 I figured that asking the community would behoove me so I can learn from peers as well.
我认为向社区询问是我应该的,这样我也可以向同行学习。 Any guidance would be greatly appreciated.
任何指导将不胜感激。
You can use Array.map
to extract the rgba
values from each object in the colorObject.colors
array and return an array of them:您可以使用
Array.map
从colorObject.colors
数组中的每个对象中提取rgba
值并返回它们的数组:
colorObject.colors.map(c => c.code.rgba)
Demo:演示:
const colorObject = { "colors": [{ "color": "black", "category": "hue", "type": "primary", "code": { "rgba": [255, 255, 255, 1] } }, { "color": "white", "category": "value", "code": { "rgba": [0, 0, 0, 1] } }, { "color": "red", "category": "hue", "type": "primary", "code": { "rgba": [255, 0, 0, 1] } }, { "color": "blue", "category": "hue", "type": "primary", "code": { "rgba": [0, 0, 255, 1] } }, { "color": "yellow", "category": "hue", "type": "primary", "code": { "rgba": [255, 255, 0, 1] } }, { "color": "green", "category": "hue", "type": "secondary", "code": { "rgba": [0, 255, 0, 1] } }, ] } console.log(colorObject.colors.map(c => c.code.rgba));
you can use Mapper and achieve this as below..您可以使用 Mapper 并按如下方式实现此目的..
const colorObject = { "colors": [{ "color": "black", "category": "hue", "type": "primary", "code": { "rgba": [255, 255, 255, 1] } }, { "color": "white", "category": "value", "code": { "rgba": [0, 0, 0, 1] } }, { "color": "red", "category": "hue", "type": "primary", "code": { "rgba": [255, 0, 0, 1] } }, { "color": "blue", "category": "hue", "type": "primary", "code": { "rgba": [0, 0, 255, 1] } }, { "color": "yellow", "category": "hue", "type": "primary", "code": { "rgba": [255, 255, 0, 1] } }, { "color": "green", "category": "hue", "type": "secondary", "code": { "rgba": [0, 255, 0, 1] } }, ] }; var newRgbaArray = colorObject.colors.map(function(rgbaItem) { return rgbaItem.code.rgba; }); console.log(newRgbaArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.