[英]How to render array entries in JSX using map() function with conditional wrapping
I have an array of objects in JavaScript and I want to loop through these objects and return some JSX with bootstrap classes in it such that each row always gets 2 columns displayed inside it.我在 JavaScript 中有一个对象数组,我想遍历这些对象并返回一些带有引导程序类的 JSX,这样每行总是在其中显示 2 列。
options: [
{
"letter":"A",
"text": "14 March 1879"
},
{
"letter":"B",
"text": "14 March 1897"
},
{
"letter":"C",
"text": "24 May 1879"
},
{
"letter":"D",
"text": "24 June 1879"
}
]
In my experience with other languages and template engines, it is pretty simple: You just create an opening and a closing div
tag with a class of row
and then using your template engine, you loop through and render each object until when the counter of your loop is 2
, you dynamically end that role and start a new one.根据我使用其他语言和模板引擎的经验,这非常简单:您只需创建一个带有row
类的开始和结束div
标签,然后使用您的模板引擎,您循环并渲染每个对象,直到您的计数器循环是2
,您动态地结束该角色并开始一个新角色。
Something like this:像这样的东西:
<div class="row">
for (var i in options) {
if(i%2 === 0) {
<!-- render object ... -->
</div><!-- then end this row -->
<div class="row"><!-- start new row -->
} else {
<!-- render object -->
}
}
</div>
I tried this method inside my map function and it was giving a syntax error because the value returned in the case where the condition passes is not valid JSX.我在我的 map 函数中尝试了这个方法,它给出了一个语法错误,因为在条件通过的情况下返回的值不是有效的 JSX。
Thanks for any help.谢谢你的帮助。
Edit:编辑:
In the End, what I want to achieve is this:最后,我想要实现的是:
If the array of objects contains 2 objects, I should be able to dynamically display it like this:如果对象数组包含 2 个对象,我应该能够像这样动态显示它:
<div class="row">
<div class="col-md-6">
<div class="option correct-option">
A <!-- that is, option.letter -->
</div>
</div>
<div class="col-md-6">
<div class="option wrong-option">
B <!-- that is, option.letter -->
</div>
</div>
</div>
if the array contains 3 objects, I want to achieve this:如果数组包含 3 个对象,我想实现这一点:
<div class="row">
<div class="col-md-6">
<div class="option correct-option">
A <!-- that is, option.letter -->
</div>
</div>
<div class="col-md-6">
<div class="option wrong-option">
B <!-- that is, option.letter -->
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="option correct-option">
C <!-- that is, option.letter -->
</div>
</div>
</div>
How about something like this? 这样的事情怎么样?
{options.map((option, i) => (
i % 2 === 0 ? null : ( // skip every 2nd row
<div class="row">
A: {option}
B: {i < options.length ? options[i + 1] : null}
</div>
)
)}
You could use a regular for loop increasing i
with 2 every loop, and check if the second option is set to take care of an array of uneven length: 您可以使用常规的for循环,使i
每次循环增加2,并检查第二个选项是否设置为照顾长度不均的数组:
Example 例
function App() {
const content = [];
for (let i = 0; i < options.length; i += 2) {
content.push(
<div class="row">
<div class="col-md-6">
<div class="option correct-option">{options[i].text}</div>
</div>
{options[i + 1] && (
<div class="col-md-6">
<div class="option correct-option">{options[i + 1].text}</div>
</div>
)}
</div>
);
}
return <div>{content}</div>;
}
HTML: HTML:
Result:结果:
Validations: Works with: Array.length = 0 & Array.length = n You want 3 Cols?... define a const instead magic number "2"....验证:适用于:Array.length = 0 & Array.length = n 你想要 3 Cols?...定义一个 const 而不是幻数“2”....
Code:代码:
const options = [
{
"letter":"A",
"text": "14 March 1879"
},
{
"letter":"B",
"text": "14 March 1897"
},
{
"letter":"C",
"text": "24 May 1879"
},
];
return (
<>
{
Array.from(Array(Math.round(options.length / 2)).keys()).map((number, index) => {
return (
<div className="row">
{
options.slice(index * 2, (index * 2) + 2).map(option=>{
return (
<div className="col-md-6">
<div className={`option ${option.true ? 'correct-option' : 'wrong-option'}`}>
{option.letter}-{option.text}
</div>
</div>
);
})
}
</div>
);
})
}
</>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.