Javascript 如何使用JSON格式获取同一节点级别的密钥值
我有这个JSON格式的结构Javascript 如何使用JSON格式获取同一节点级别的密钥值,javascript,Javascript,我有这个JSON格式的结构 valuesColors : [{ key: "<75%", color:"61C56E" }, { key: ">=75%&<90%", color:"6144RF" }, { key: ">90%", color:"333RTE" } ] 值颜色:[{ 关键字:“=75%和90%”, 颜色:“333RTE” } ] 例如,我会得到valuesColors.c
valuesColors : [{
key: "<75%",
color:"61C56E"
},
{
key: ">=75%&<90%",
color:"6144RF"
},
{
key: ">90%",
color:"333RTE"
}
]
值颜色:[{
关键字:“=75%和90%”,
颜色:“333RTE”
}
]
例如,我会得到valuesColors.color的valuesColor.key==>75%。这里的问题是,我的值与键的级别相同,因此我不能使用
您不能使用。因为您的对象是数组类型,而该数组中的每个元素都是json节点。因此,您需要访问相关索引,然后才能对对象进行操作
let array = [{key: '1'}, {key: '2'}];
let jsonNode = array[0];
console.log(jsonNode.key);
console.log(array[0].key);
console.log(array[1].key);
console.log(array.key); // Will not work as this is an array, not a json object.
:
const result=valuesColors.find(条目=>{
return entry.key==“您想只获取值>75%和90%的第一个键吗?请阅读
const result = valuesColors.find(entry => {
return entry.key == "<75%"; // or what ever logic
});
console.log(result.color); // -> 61C56E