0

I have an object each key of the object has an array value

const correctionsWords  = {
"word": [ "1" , "2"] ,
"word2": ["20" ,"22" ] 
};

I did map through each key by using the following code

let correctionList =  Object.keys(correctionsWords).map(  (key)  => {
        return (
          <div>
              {
                  //console.log( 'After Mapping ' , correctionsWords[key]) [1,2,3]
                   <ul>
                     <li>{key} { /* word  */}
                      <ul> 
                        <li>{correctionsWords[key]}</li>
                      </ul>
                     </li>  
                     </ul>
              } 
          </div> 
        ); });

the result is * key: word * value: 1 2 How can I list the values of the array?

2 Answers 2

1

Map again each array element:

<ul>
  {correctionsWords[key].map(el => (
    <li key={el}>{el}</li>
  ))}
</ul>

I've used a key as the element here. If the elements are not unique better use another key. Also, you need another key for your object mapping in the topmost div:

return (
    <div key={key}>
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

You are welcome. If the answer helped you consider accepting it, please.
Sure It did help me
@RamiSalim Look at the up and down arrow, there is a checkmark, press it.
"To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in." stackoverflow.com/help/someone-answers :)
1

I think what you’re looking for is to replace that innermost li with:

{
  correctionsWords[key].map(value => <li>{value}</li>)
}

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.