5

I'm mapping through data in Reactjs. This JSX:

{place.venue.location.formattedAddress}

Is mapping through this Axios data request, specially, this array in my response:

formattedAddress: Array(5)
0 : "Chester Rd"
1 : "London"
2 : "Greater London"
3 : "NW1 4NR"
4 : "United Kingdom"

As a result, when I'm mapping, it returns the data, in one sentence, all joined together. Exactly like this:

Serpentine RdHyde ParkGreater LondonW2 2TP

I'm trying to map through, split and join with a line break, but it's not working. My address stops appearing completely. What am I doing wrong?

Here's my code, (I've sliced, so I can remove the country off the end of the address).

{place.venue.location.formattedAddress.slice(0,4).split(',').join('<br />')}
2
  • 1
    Only strings have a .split method. If formattedAddress is an array, then formattedAddress.slice(...) returns another array. Just drop the .split. Commented Jun 5, 2018 at 21:40
  • perfect, thank you @FelixKling! Commented Jun 5, 2018 at 21:46

3 Answers 3

7

class App extends React.Component{
  render(){
    var arr = ["hello", "there", "world"]
    return(
      <div>
        {arr.map(s=><React.Fragment>{s}<br/></React.Fragment>)}
      </div>
    )
  }
}

ReactDOM.render(<App />, document.getElementById("app"))
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="app"></div>

1 You are calling split on array, but it is a string method.

2 You cant use join using '<br />'. jsx will treat it as string.

you should map over your array elements and return an array of jsx.

{place.venue.location.formattedAddress.slice(0,4).map(s=><React.Fragment>s <br/></React.Fragment>)}
Sign up to request clarification or add additional context in comments.

2 Comments

This is not quite like join as join would not append a <br /> to the end of the array of elements
@Trevor Here is how to do it without final <br /> stackoverflow.com/a/68680034/4840661
3

Using map without generating an ending break.

const breakedAttributes = attributes.map((attribute, index) => {
    const isLast = attributes.length === (index + 1);
    return !isLast ? <>{attribute}<br/></> : attribute;
})

Comments

0

Since the accepted answer's method will leave a trailing <br />, I'd share my approach.


Use reduce() on your array to insert a <br /> between each item, since React can render Array's without any issues, this will result in the expected behaviour:

{arr.reduce((p, c) => [ p, <br />, c ])}
<div>
    hello
    <br>
    there
    <br>
    world
</div>

React 17 Demo

const { useState } = React;

const Example = () => {

    const arr = [ "hello", "there", "world" ];

    return (
        <div>
            {arr.reduce((p, c) => [ p, <br />, c ])}
        </div>
    )
}
ReactDOM.render(<Example />, document.getElementById("react"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

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.