2

I have json data for order.expeditionPlaces which is formatted like:

"expeditionPlaces": "Place1, Place2",

I am trying to split this so that it will display each place on a new line and I also need to be able to check if it is null

Place1

Place2

{order.expeditionPlaces.split(",").join("<br />")} threw an error when it received a null value.

I'm not sure what the best approach to this is, I've tried split and join on the object itself and also tried to put it into const Split but it doesn't like this, I'm not sure where it should go within the if statement?

if (this.state.possibleOrders && this.state.possibleOrders.length > 0) {
        this.state.possibleOrders.forEach((order, index) => {
            possibleOrders.push(<tr key={index}>
                <td>{order.orderId}</td>
                <td><Button bsStyle="success">{order.sortingTableName}</Button></td>
                <td>{Split}</td>
                <td>{order.expeditionPlaces.split(",").join("<br />")}</td>
                <td>{order.sortingBufferPlaces}</td>
            </tr>);

            const Split = if (order.expeditionPlaces.length >0) {order.expeditionPlaces.split(",").join("<br />")}}
        }
    );

1 Answer 1

1

Just replace this

{order.expeditionPlaces.split(",").join("<br />")}

With

{order.expeditionPlaces ? order.expeditionPlaces.split(",").join("<br />") : ''}
Sign up to request clarification or add additional context in comments.

1 Comment

@ThomasHarrison Please accept the answer if it worked

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.