【发布时间】:2020-07-13 06:31:13
【问题描述】:
我想从以下数组中检索所有文本(文本 1、文本 2....):
[
{
"reviews":
[
{
"_id": "5e84239d6e24358b50f3fe4e",
"text": "My text 1"
},
{
"_id": "5e8423a46e24358b50f3fe4f",
"text": "My text 2"
}
]
},
{
"reviews":
[
{
"_id": "5e84239d6e24358b50f3fe4e",
"text": "My text 3"
},
{
"_id": "5e8423a46e24358b50f3fe4f",
"text": "My text 4"
}
]
},
{
"reviews":
[
{
"_id": "5e84239d6e24358b50f3fe4e",
"text": "My text 5"
},
{
"_id": "5e8423a46e24358b50f3fe4f",
"text": "My text 6"
}
]
}
]
这个数组存储在一个名为stores 的变量中。
我尝试了以下方法:
const listText = stores.map(count => count.reviews.text // [null, null, null]const listText = stores.map((count, i) => count.reviews[i].text) // 无法读取 undefined 的属性 'text'const listText = stores.forEach((key, i) => key.reviews[i].text) // 无法读取 undefined 的属性 'text'
你能帮帮我吗? 非常感谢
【问题讨论】:
标签: javascript arrays loops object nested