循环嵌套数组

时间:2016-10-28 20:55:36

标签: javascript arrays reactjs multidimensional-array

我有一个嵌套数组,如下所示。我试图首先循环顶级数组并获取groupValue,然后为每个组值我需要循环遍历docs数组并获取每个文档的标题。

嵌套数组示例:

[ { 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] } } ]

预期输出:

Heading1:
iphone
samsung

Heading2:
motorola
ipad

我尝试了以下方法,并且我能够返回所有子元素,但我不确定如何从父数组返回标题以及相应的子元素。

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>);
    })
});

3 个答案:

答案 0 :(得分:2)

这是一个应该呈现你想要的例子:

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>

答案 1 :(得分:0)

<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>

答案 2 :(得分:0)

您还可以将数据提取与渲染分开,这将使其更具可读性。

function prepareData(groups) {
  return groups.map(group => {
    return {
      heading: group.groupValue,
      items: group.doclist.docs.map(doc => doc.title)
    };
  });
}

现在渲染

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>
  );
}