Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a 2d array in React Js

Tags:

reactjs

Projects.js

 constructor() {
  super();
  this.state = {
     projects: [] 
  }
}

componentWillMount() {
  this.setState({
     projects: [
        {
           link: '/',
           img: 'img.jpg',
           title: 'Title',
           date: 'Jul 2017',
           description: 'Description',
           icons: 
           [
              {
                 src: 'icon.svg',
                 content: 'Content'
              },
              {
                 src: 'icon2.svg',
                 content: 'Content 2'
           ]
        }
     ]
   });
  }

render() {
   return (
      <section className="projects">
         <ProjectsList projects={this.state.projects} />
      </section>
   );
  }
}

ProjectsList.js

let projectItems;

projectItems = this.props.projects.map(project => {
  return (
    <ProjectItem key={project.title} project={project}  />
  ) 
});

return (
   <div id="projectsList">
     {projectItems}
   </div>
)

I want to create a project card that has a list of icons on the bottom. I can map the projects array but the icons won't work. Anyone know how to map this 2d array in ReactJs? Thanks!

like image 250
gabes graphic Avatar asked Mar 03 '26 00:03

gabes graphic


2 Answers

Simply use map again:

projectItems = this.props.projects.map(project => {
  return (
    <div>
      <ProjectItem key={project.title} project={project}  />
      { 
        project.icons.map(i => <img key={i.src} src={i.src} alt="" />) 
      }
    </div>
  ) 
});

You can do it of course inside ProjectItem

like image 149
Tomasz Mularczyk Avatar answered Mar 06 '26 05:03

Tomasz Mularczyk


array = [[null,null,null],[null,null,null]]

return (
<div> {array.map(x=> x.map(y=> <div>{....}</div> ))}</div>
)

mapping it again inside the array will give the value

like image 23
Om Fuke Avatar answered Mar 06 '26 06:03

Om Fuke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!