【发布时间】:2019-05-30 07:54:11
【问题描述】:
我有一个 json 文件,我用 map 对其进行迭代,但我有一个名为“content”的深层对象键,当我用 map 返回我的 json 数组时,我想从中获取特定的键
JSON:
{
"item": [
{
"title": "...",
"link": "...",
"content": {
"_url": "How can I get this :D",
...
}
}
]
}
我的 javascript 代码:
getAllNews = async () => {
const result = await this.getResource('item/')
return result.map(this._transformPosts)
// Here I iterate my json
}
_transformPosts = (item) => {
return {
title: item.title,
link: item.link,
image: item.content._url,
// and here I try get _url
id: item.guide,
}
}
【问题讨论】:
-
结果应该是什么?
-
你有一个错字:
item.conten._url -
我知道这不会改变任何事情
-
我想将`content._url`返回到
image? -
image: (item.content && item.content._url) ? item.content._url : ''应该足够了。
标签: javascript arrays json object iteration