[英]Looping through Nested array
I have a nested array as below. 我有一个嵌套的数组,如下所示。 I am trying to first loop the top level array and get the groupValue and then for every group value I need to loop over the docs array and get the title for each document.
我试图首先循环顶级数组并获取groupValue,然后对于每个组值,我都需要循环遍历docs数组并获取每个文档的标题。
Nested array example: 嵌套数组示例:
[ { groupValue: 'Heading1',
doclist: { numFound: 958, start: 0, maxScore: 1.2041028, docs: [Object] } },
{ groupValue: 'Heading2',
doclist: { numFound: 8700, start: 0, maxScore: 0.50214005, docs: [Object] } },
{ groupValue: 'Heading3',
doclist: { numFound: 1138, start: 0, maxScore: 0.48008662, docs: [Object] } } ]
Expected Output: 预期产量:
Heading1:
iphone
samsung
Heading2:
motorola
ipad
I tried the below approach and I was able to return all child elements but I am not sure how to return the heading from the parent array along with the corresponding child elements. 我尝试了以下方法,但能够返回所有子元素,但不确定如何从父数组返回标题以及相应的子元素。
var values=groups.map(function(item,i) {
let docs=groups[i].doclist.docs
console.log(groups[i].groupValue);
return docs.map(function(item,i) {
return (<div key={i}>{docs[i].title}</div>);
})
});
Here's an example that should render what you wanted: 这是一个应呈现所需内容的示例:
class Example extends React.Component { render() { const groups = [{ groupValue: 'Heading1', doclist: { numFound: 958, start: 0, maxScore: 1.2041028, docs: [{title: 'iphone'}, {title: 'samsung'}] } }, { groupValue: 'Heading2', doclist: { numFound: 8700, start: 0, maxScore: 0.50214005, docs: [{title: 'motorola'}, {title: 'ipad'}] } }]; let values = groups.map((item, i) => { let docs = groups[i].doclist.docs; let doc = docs.map((item, i) => <li key={i}>{docs[i].title}</li>); return ( <div> <h3>{groups[i].groupValue}</h3> <ul>{doc}</ul> </div> ); }); return <div>{values}</div>; } } ReactDOM.render(<Example/>, document.getElementById('View'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.min.js"></script> <div id="View"></div>
<script type="text/javascript">
var groups = [ { groupValue: 'Heading1',
doclist: { numFound: 958, start: 0, maxScore: 1.2041028, docs: [Object] } },
{ groupValue: 'Heading2',
doclist: { numFound: 8700, start: 0, maxScore: 0.50214005, docs: [Object] } },
{ groupValue: 'Heading3',
doclist: { numFound: 1138, start: 0, maxScore: 0.48008662, docs: [Object] } }];
// NORMAL LOOPING USING forEach
groups.forEach(function(a, b){
var headings = a.groupValue;
var docList = a.doclist;
var docs = docList.docs;
var start = docList.start;
var maxScore = docList.maxScore;
var numFound = docList.numFound;
var docTitle = docs.title;
// YOU CAN USE THE VALUES AS YOU WISH...
console.log(docs);
});
// USING ARRAY map FOR REACT.JS
var values = groups.map(function(item, i) {
let docs = item.doclist.docs;
let headings = item.groupValue;
let doc = docs.map(function(objDoc, n){
return (<p key={n}>{objDoc.title}</p>);
});
return (
<div key={i}>
<h3>{headings}</h3>
{doc}
</div>
);
});
</script>
You could also separate data extraction from render, which will make it more readable. 您还可以将数据提取与渲染分开,这将使其更具可读性。
function prepareData(groups) {
return groups.map(group => {
return {
heading: group.groupValue,
items: group.doclist.docs.map(doc => doc.title)
};
});
}
Now render 现在渲染
render() {
const groups = [
{ groupValue: 'Heading1',
doclist: { numFound: 958, start: 0, maxScore: 1.2041028, docs: [Object] } },
{ groupValue: 'Heading2',
doclist: { numFound: 8700, start: 0, maxScore: 0.50214005, docs: [Object] } },
{ groupValue: 'Heading3',
doclist: { numFound: 1138, start: 0, maxScore: 0.48008662, docs: [Object] } }];
const dataToShow = prepareData(groups);
return (
<div>
{
dataToShow.map(group =>
<h3>{group.heading}</h3>
<ul>{group.items.map(item => <li>{item}</li>}</ul>
)
}
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.