Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to do array.join in react

I'd like to inject a separator between each element of an array using an array.join-like syntax.

Something like the following:

render() {  let myArray = [1,2,3];  return (<div>    {myArray.map(item => <div>{item}</div>).join(<div>|</div>)} </div>); } 

I've got it working using a lodash.transform approach, but it feels ugly, I'd like to just do .join(<some-jsx>) and have it insert a separator in between each item.

like image 946
Ben Avatar asked Nov 06 '15 23:11

Ben


People also ask

Can I use splice in React?

However, splice will change the contents of the array it's called on. You need to set the state on the updated array, not on what splice returns. Below is a working snippet to demonstrate how you can insert a new element at a selected index using splice within a React component.

What is && operator in React?

Inline If with Logical && Operator It works because in JavaScript, true && expression always evaluates to expression , and false && expression always evaluates to false . Therefore, if the condition is true , the element right after && will appear in the output. If it is false , React will ignore and skip it.

Which method joins all array elements into string with specified separator?

join() The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.

Can you have an array of React components?

To render an array of components in React you simply need to pass the array into JSX by wrapping it in curly braces, just be sure that your components each have a unique key prop because React will use this when rendering it to avoid bugs.

How to join array elements with react elements?

Trying to .join with React Elements is probably not going out pan out for you. This would produce the result you describe needing. Show activity on this post. You can also do it by combining .reduce and React fragments. Show activity on this post. Using Array.map and React.Fragment you can join each array item with any JSX element you want.

How to manipulate array in ReactJS?

We can use the JavaScript standard Array functions in the ReactJS. However, we have to do some extra work to manipulate the Array in ReactJS, why because the ReactJS uses the useState Hook to update the variable values. In this tutorial, we are going to see the following topics. Iterate String and Array of Objects. Add a new row to Array.

How do you loop through an array in react JS?

Using something like a loop against an array or an object means you only have to write the HTML per item one time. Better yet, any future edits only have to be applied once. To render multiple JSX elements in React, you can loop through an array with the .map () method and return a single element.

How to render multiple JSX elements in react?

Better yet, any future edits only have to be applied once. To render multiple JSX elements in React, you can loop through an array with the .map () method and return a single element.


1 Answers

You can also use reduce to insert the separator between every element of the array:

render() {   let myArray = [1,2,3];   return (     <div>       {         myArray           .map(item => <div>{item}</div>)           .reduce((acc, x) => acc === null ? [x] : [acc, ' | ', x], null)       }     </div>   ); } 

or using fragments:

render() {   let myArray = [1,2,3];   return (     <div>       {         myArray           .map(item => <div>{item}</div>)           .reduce((acc, x) => acc === null ? x : <>{acc} | {x}</>, null)       }     </div>   ); } 
like image 144
Bless Avatar answered Sep 17 '22 12:09

Bless