简体   繁体   中英

using split and map in jsx

const arr = ['name', 'contact number']

const App = () => (
  <div style={styles}>
  Add {arr.split(',').map(o=>o)}
  </div>
);

why this won't work? I want to print Add name & contact, but stuck at splitting it.

You're using 2 functions wrong:

  1. split is supposed to be used to split a string into an array, around the provided character. You already have the resulting array.
  2. .map(o=>o) is useless - it basically returns the same array provided.

You're probably looking to do this Add {arr.join(' & ')} .

You are looking to join the values

Add {arr.join(',')}

Below links should help you

MDN split

MDN join

There is nothing to split. I think you are trying to join them:

const arr = ['name', 'contact number']

const App = () => (
  <div style={styles}>
  Add {arr.join(',')}
  </div>
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM