-2

at first please see image then, how can use for example:
use this humans.body."name" instead of this humans.body.name? is there any way to solve that?

enter image description here

my codes:

//this is my file
let fileJson = {
status: 200,
result: {
author_id: "137",
text: "1281ms",
author: "Ctrl+Z",
},
};

let data = [{id: 1, key: "author_id"}];

console.log(fileJson.result.author_id); //output: 137
console.log(data[0].key); //output: author_id
console.log(fileJson.result.data[0].key); //output: Uncaught TypeError ...

//how can solve this error?

thank you :)

Ehs4n
  • 137
  • 8
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 27 '22 at 18:49

2 Answers2

1

try using this notation to access the property:

fileJson.result[data[0].key]
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • 1
    excellent! glad to help. you may want to mark the answer as "accepted" if it has solved your problem. – dqhendricks Sep 27 '22 at 18:54
  • _"you may want to mark the answer as "accepted""_ - You might want to close this as dupe instead of adding an answer. – Andreas Sep 27 '22 at 18:57
1

Values can be accessed a couple of ways. Use the array accessor with the key value.

//this is my file
let fileJson = {
  status: 200,
  result: {
    author_id: "137",
    text: "1281ms",
    author: "Ctrl+Z",
  },
};

let data = [{
  id: 1,
  key: "author_id"
}];

console.log(fileJson.result.author_id); //output: 137
console.log(data[0].key); //output: author_id
console.log(fileJson.result[data[0].key]);
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100