1

I have an array of objects (here is only one but thats does not matter) in my JSON "dummy" back end. In each object I have a property "tags" which also contains simple array, let me show you an example

[
 {
  "title": "Get up and run",
  "author": "Johnny",
  "tags": ["react", "javascript"]
 }
]

I tried to map an array which gave me a result: (see the code)

Article title: Get up and run
Writer: Johnny
Tags: reactjavascript

but i want to get result like that:

Article title: Get up and run
Writer: Johnny
Tags: react javascript (or "react, javascript" or "#react #javascript")

it seems I can't map "tag" array and main array of objects correctly same time. :( can u help me?

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>
    )
}
}

I expect the better "map" of array I try to get result like this

Article title: Get up and run Writer: Johnny

Tags: react javascript (or "react, javascript" or "#react #javascript") 

instead of

Tags: reactjavascript

or

Tags: ["react", "javascript"] (it was the worst version :)(its fixed ;) ) )

I want to map an array of objects and 'tags' array at same time and correctly,

How can I do that?

3 Answers 3

1

Doing tags={post.tags.map(xTag => { return xTag ; })} is equivalent to tags={post.tags}. So the relevant code that affects the final formatting is in Post component. So could either do tags.join(', ') or tags.map(t => '#'+t).join(' ').

Sign up to request clarification or add additional context in comments.

Comments

0

You can create a separate component for tag and style it. Then rewrite this line:

tags={post.tags.map(xTag => {
    return <Tag>{xTag}</Tag>;  // or <Tag content={xTag} />
})}

Comments

0

If you want to convert your tags array into a string there are several options depending on your desired format:

Using a simple join your formatting is limited: (You can pass in characters which will be appended after every entry e.g .join(' ') for only spaces)

post.tags.join() // Returns 'react,javascript'

Using the reduce function you have further control over how your string will be formatted:

post.tags.reduce((string, tag) => string += `${tag} `, '')

To change the format just edit the string which is attached ${tag}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.