简体   繁体   English

如何为数组中另一个数组中的对象中的每个对象分配唯一键和线性键?

[英]How can I assign unique and linear key for each object within an array that's in an object within another array?

The dataset that I'm working with is structured like this: 我正在使用的数据集的结构如下:

在此处输入图片说明

After running my code, I'm getting this result: 运行代码后,我得到以下结果:

在此处输入图片说明

But I need the objects' keys in the "Dessert" to be 3, 4, 5 instead of relooping from 0 after the first iteration. 但是我需要将“ Dessert”中对象的键设置为3、4、5,而不是在第一次迭代后从0重新循环。

My current code looks like this (sorry for the indentation- it's acting weird): 我当前的代码看起来像这样(缩进很抱歉-行为很奇怪):

 const groupSource = [ { title: 'Fruits', group: [ {text: 'apple'}, {text: 'banana'}, {text: 'grapefruit'} ], }, { title: 'Desserts', group: [ {text: 'icecream', color: 'orange'}, {text: 'chocolate'}, {text: 'chips'} ], } ]; class ResultCategory extends React.Component{ render(){ return( <div> {this.props.text} </div> ); } } class Result extends React.Component{ render(){ return( <table> <tbody> <tr><td>Name:</td><td> {this.props.text} </td></tr> <tr><td>Key:</td><td> {this.props.keyz} </td></tr> </tbody> </table> ); } } class Search extends React.Component{ render(){ var database = this.props.database; var ResultsDisplay = database.map((object, index) => { return( <div> <div className="Category"> <ResultCategory text={object.title} /> </div> {object.group.map((item, index)=>{ return( <li className="results" onClick={this.onClick}> <Result text={item.text} keyz={index} /> </li> ); })} </div> ); }); return( <div> {ResultsDisplay} </div> ); } } ReactDOM.render( <Search database={groupSource} />, document.getElementById('container') ); 
 *{ font-family: Helvetica; font-weight: 100; } li{ list-style: none; } .Category{ margin-top: 20px; margin-bottom: 5px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="container"> <!-- This element's contents will be replaced with your component. --> </div> 

Thanks in advance! 提前致谢!

The container component (Search) can track the index counting you want. 容器组件(搜索)可以跟踪所需的索引计数。

 const groupSource = [ { title: 'Fruits', group: [ {text: 'apple'}, {text: 'banana'}, {text: 'grapefruit'} ], }, { title: 'Desserts', group: [ {text: 'icecream', color: 'orange'}, {text: 'chocolate'}, {text: 'chips'} ], } ]; class ResultCategory extends React.Component{ render(){ return( <div> {this.props.text} </div> ); } } class Result extends React.Component{ render(){ return( <table> <tbody> <tr><td>Name:</td><td> {this.props.text} </td></tr> <tr><td>Key:</td><td> {this.props.keyz} </td></tr> </tbody> </table> ); } } class Search extends React.Component{ render(){ var database = this.props.database; var totalIndex = 0; var ResultsDisplay = database.map((object, index) => { return( <div> <div className="Category"> <ResultCategory text={object.title} /> </div> {object.group.map((item, index)=>{ return( <li className="results" onClick={this.onClick}> <Result text={item.text} keyz={totalIndex++} /> </li> ); })} </div> ); }); return( <div> {ResultsDisplay} </div> ); } } ReactDOM.render( <Search database={groupSource} />, document.getElementById('container') ); 
 *{ font-family: Helvetica; font-weight: 100; } li{ list-style: none; } .Category{ margin-top: 20px; margin-bottom: 5px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="container"> <!-- This element's contents will be replaced with your component. --> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个对象分配给另一个对象中的数组? - How do I assign an object to an array within another object? 如何将对象数组中的 object 推送到另一个 object 数组中的数组? - How can I push an object in an array of objects to an array within another array of object? 如何动态更新数组中另一个对象内的嵌套对象? - how can i dynamically update my nested object within another object, within an array? 将 object 分配给另一个 object 中的密钥 - Assign an object to a key within another object 如何检查对象中的字符串是否在数组中是唯一的 - How to check if string within object is unique in array 如何找到一个在其数组中包含值的对象键? - How to find an object key that contains a value within it's array? JQUERY .each如何到达对象内的数组? - JQUERY .each how to reach array within object? 如何在JavaScript中为数组中的每个对象分配一个随机数? - How can I assign a random number to each object in an array in javascript? 根据数组中 object 的索引,为数组中的每个 object 分配一个具有唯一 id 的新键 - Assign a new key to each object in the array with a unique id based on the index of the object in the array 如何将对象数组分配给数组? - How can i assign array of object to array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM