Javascript 获取父数组对象中当前元素的数目
我有以下JSON对象,在构建Message.jsx时,我希望在节数组中包含当前消息元素的编号作为id。我怎么能这么做Javascript 获取父数组对象中当前元素的数目,javascript,reactive-programming,jsx,Javascript,Reactive Programming,Jsx,我有以下JSON对象,在构建Message.jsx时,我希望在节数组中包含当前消息元素的编号作为id。我怎么能这么做 { "sections": [ { "message": { "from": "someone", "body": "ibsum" } }, { "message": { "from": "someone else", "body": "lorem"
{ "sections": [
{
"message": {
"from": "someone",
"body": "ibsum"
}
},
{
"message": {
"from": "someone else",
"body": "lorem"
}
}, ... ] }
我是否可以在不显式设置节数组中每个对象的id属性的情况下执行此操作?使用
map
的第二个属性,它是数组中的元素索引。您可以用同样的方法设置关键点。记住,索引从0开始
比如说:
<Section>
{this.props.messages.map((message, i) => {
return <Message key={i} id={i} info={message} />
})}
</Section>
{this.props.messages.map((message,i)=>{
返回
})}