1

Need to split the value using hypen in map Result object

{ 
    first: "test-123",
    second: "second-6" 
    seconds: "bet-80"
    days: "4-7", 
}

expected

<div>
    <div> 
        <label>first : </label>
        <label>test</label>
        <label>123</label>
    </div>
    <div>
        <label>second : </label>
        <label>second</label>
        <label>6</label>
    </div>
    <div>
        <label>seconds : </label> 
        <label>bet</label>
        <label>80</label>
    </div>
    <div>
        <label>days : </label> 
        <label>4</label>
        <label>7</label>
    </div> 
</div>

my code

<div>
{Object.keys(codes).map((key) => (
 
           <label>{key} : </label> 
         <label>codes[key]</label> 
         <label> </label> 
          ))}
          
    </div>

how to split hypen from the values and displayed in two different labels

test-123 split hypen symbol and try to displayed like below

 <div> 
            <label>first : </label>
            <label>test</label>
            <label>123</label>
        </div>
    

3 Answers 3

2

Is it what you want?

         Object.keys(codes).map((key) => {
 
          let [a, b] = codes[key].split("-");
           return(
           <div key={codes[key]}>
             <label>{key} : </label> 
             <label>{a}</label> 
             <label>{b}</label> 
           </div>
          })}
          )
}
Sign up to request clarification or add additional context in comments.

Comments

1
{
  Object.keys(codes).map((key) => (
    <div>
      <label>{key} : </label>
      {codes[key].split("-").map((val) => (
        <label>{val}</label>
      ))}
    </div>
  ));
}        

Comments

0

You can try this as the result is exactly what you want.You can get the string or value before the first dash by codes[key].split('-')[0] and after the dash by codes[key].split('-')[1]

state = {
   codes : {
    first: "test-123",
    second: "second-6" ,
    seconds: "bet-80",
    days: "4-7"
   }
  }
  render(){
    const {codes} = this.state
    let first,second
    const render =  Object.keys(codes).map((key) => [
    <>
    <label>{key} </label> 
    <label>{first = codes[key].split('-')[0]}</label> 
    <label>{second = codes[key].split('-')[1]}</label> 
    </>
    ])

  return (
    
  <>{render}</>
  );
}

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.