【发布时间】:2019-11-11 10:27:48
【问题描述】:
我的 JSON“虚拟”后端中有一组对象(这里只有一个,但没关系)。 在每个对象中,我都有一个属性“tags”,其中还包含简单的数组,让我给你看一个例子
[
{
"title": "Get up and run",
"author": "Johnny",
"tags": ["react", "javascript"]
}
]
我试图映射一个给我结果的数组:(见代码)
Article title: Get up and run
Writer: Johnny
Tags: reactjavascript
但我想得到这样的结果:
Article title: Get up and run
Writer: Johnny
Tags: react javascript (or "react, javascript" or "#react #javascript")
似乎我无法同时正确映射“标记”数组和对象的主数组。 :( 你能帮帮我吗?
class Content extends Component {
state = {
posts: []
}
componentDidMount () {
axios.get("json-file.json")
.then(response => {
this.setState({posts: response.data.map(post => {
return post; /*here I get the whole data from dummy
backend */
})})
}
)
}
render () {
const post = this.state.posts.map(post => {
return <Post
title={post.title}
date={post.date}
author={post.author}
tags={post.tags.map(xTag => {
return xTag ;
})} /* I have other Component which correctly renders this...
no worries here */
/>
})
return (
<div>
{post}
</div>
)
}
}
我期待更好的数组“地图” 我试图得到这样的结果
文章标题:起来跑 作者:强尼
Tags: react javascript (or "react, javascript" or "#react #javascript")
而不是
Tags: reactjavascript
或
Tags: ["react", "javascript"] (it was the worst version :)(its fixed ;) ) )
我想同时正确地映射一个对象数组和“标签”数组,
我该怎么做?
【问题讨论】:
标签: javascript arrays reactjs object ecmascript-6