[英]Mapping an object of array in Reactjs
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? 结果是*键:word *值:1 2如何列出数组的值?
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. 我在这里使用了一个
key
作为元素。 If the elements are not unique better use another key. 如果元素不是唯一的,最好使用另一个键。 Also, you need another key for your object mapping in the topmost
div
: 另外,您还需要在最顶层的
div
用于对象映射的另一个键:
return (
<div key={key}>
...
I think what you're looking for is to replace that innermost li
with: 我认为您正在寻找的是将最里面的
li
替换为:
{
correctionsWords[key].map(value => <li>{value}</li>)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.