[英]Access properties of objects into nested Array - Javascript
我想从以下数组中检索所有文本(文本 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)
// 无法读取未定义的属性 'text'
const listText = stores.forEach((key, i) => key.reviews[i].text)
// 无法读取未定义的属性 'text'
你能在这里帮我一下吗,非常感谢
您不能使用count.reviews.text
因为评论是一个数组,因此您还应该遍历评论:
const data = [ { "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" } ] } ] const listText = data.reduce((acc,current) => acc.concat(current.reviews.map(it => it.text)), []) console.log(listText)
地图和平面:
const data = [ { "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" } ] } ] console.log( data.map(items => items.reviews.map(review => review.text)).flat() )
使用.flatMap()
,您可以遍历每个对象,然后.map()
将每个对象的评论数组迭代为包含文本属性的数组。 .flatMap()
允许您将所有内部地图数组结果展平为一个更大的结果数组。
const stores = [ { "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" } ] } ]; const res = stores.flatMap((o) => o.reviews.map(({text}) => text)); console.log(res);
如果你不能支持.flatMap()
,你总是可以使用.map()
映射到一个数组数组,然后在使用.concat()
和扩展语法( ...
) 后将其展平:
const stores = [ { "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" } ] } ]; const res = [].concat(...stores.map((o) => o.reviews.map(({text}) => text))); console.log(res);
试试这个:
let output = [];
input.map(function (item_1) {
item_1.reviews.map(function (item_2) {
output.push(item_2.text);
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.