访问数组内的对象-Javascript [英] Access object inside an array - Javascript
本文介绍了访问数组内的对象-Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的回复正文. 它存储在变量中,当我使用 console.log(body)我得到以下内容.
This is how my response body looks like. Its stored in a variable and when i use console.log(body) I get the following.
[
{
"key1":"value1",
"key2":"value2",
"key3":"value3"
}
]
我正在尝试使用以下
console.log(body[0].key3)
我不确定.我不确定这里出什么问题了.如果我只是做
I get undefined. Im not sure what is wrong here. If i just do
console.log(body[0])
我正在获取字符串[
感谢您的帮助.
推荐答案
问题已解决
您的JS正在查看特定字符上的属性:
The Problem Explained
Your JS is looking at the property on a specific character:
看看下面的示例,该示例将有助于演示正在发生的事情:
Take a look at the following example that will help demonstrate what is going on:
const string = 'Hello';
console.log(string[0] === 'H'); // true
console.log('H'.key3 === undefined); // true
您需要JSON.parse
字符串:
const body = `
[
{
"key1":"value1",
"key2":"value2",
"key3":"value3"
}
]
`;
const parsed = JSON.parse(body);
console.log(parsed[0].key3);
这篇关于访问数组内的对象-Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文