javascript - 如何在 object 中显示元素

我在 javascript 中有一个 object。我怎样才能在console.log中只显示“category =”hue”?

let colorlist = {
"colors": [
{
"color": "black",
"category": "hue", // only show this in console.log
"type": "primary",
"code": {
"rgba": [255,255,255,1],
"hex": "#000"
}
},
{
"color": "white",
"category": "value",
"code": {
"rgba": [0,0,0,1],
"hex": "#FFF"
}
},

回答1

你可以做这样的事情

colorlist.colors.map((item) => {
    for (let [key, value] of Object.entries(item)) {
        if(value === "hue"){
            console.log(`"${key}" : "${value}"`);
        }
    }
})

输出将是

"category" : "hue"

相似文章