0

I have this JSON format structure

  valuesColors : [{
    key: "<75%",
    color:"61C56E"
  },
  {
    key: ">=75%&<90%",
    color:"6144RF"
  },
  {
    key: ">90%",
    color:"333RTE"
  }
]

I would get for exemple valuesColors.color of valuesColor.key == ">75%". the problem here I have the value in the same level of the key so I can't use .

str
  • 42,689
  • 17
  • 109
  • 127
infodev
  • 4,673
  • 17
  • 65
  • 138
  • you want to get only first key of value >75%&90%? – jvk Aug 08 '18 at 15:19
  • Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – str Aug 08 '18 at 15:54

2 Answers2

0

You can't use . because your object is an array type and each element in that array is a json node. So you'd need to access the relevant index and then you can operate on the object.

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.
Adam
  • 1,724
  • 13
  • 16
0

Array.find():

const result = valuesColors.find(entry => {
  return entry.key == "<75%"; // or what ever logic
});

console.log(result.color); // -> 61C56E

https://stackblitz.com/edit/how-to-get-value-of-key-in-same-node-level-with-json-format?file=index.js

Wilhelmina Lohan
  • 2,803
  • 2
  • 29
  • 58