1

I am getting map inside an array in redux store from spring controller. And i want to map through that array. but its giving me error TypeError: project_tasks.map is not a function. I can see map inside state but i am not able to map through that array. How can i do this please help me i am new to react.

this is my store

store

mobile.js

let mobile = [];

const mobiletasks = project_tasks.map((project_task, index) => (
<mobileItems key={project_task.viewproducts3.id} project_task={project_task} />
));  

mobile.push(mobiletasks);

mobileItems.js

import React, { Component } from 'react'

class mobileItems extends Component {
render() {
const { project_task } = this.props;
return (
   <div class="row mx-auto" style="background-color: white;">
       <div class="col-lg-2 col-md-6 mb-4">
          <a href="" style="text-decoration: none;" id="">
              <div class="card h-100">
                  <img src="" alt="" class="card-img-top zoom mt-2"/>
                  <div class="card-body text-center">
                      <h5 class="titlename text-truncate">project_task.name</h5>
                      <div class="caption">
                          <h5 class="pull-right productprice">&#8377;</h5>
                       </div>
                  </div>
              </div>
          </a>
      </div>
     </div>
);
}
}

export default mobileItems;

Please tell me what am i doing wrong here?

3
  • Possible Duplicate of React - how to map nested object values? Commented Jan 2, 2019 at 5:56
  • Check if project_tasks is an array. If it's not an array, then map function cannot be applied to that property(i.e. project_tasks). Commented Jan 2, 2019 at 7:52
  • @Shubham Khatri i tried as you said but. its showing me output 5 times on console. Commented Jan 2, 2019 at 8:46

5 Answers 5

1

try this

{Object.keys(project_tasks).map((item, index) => {
if(item == "viewproducts3"){
return (project_tasks.viewproducts2.map((c) =>  {
return (
   <h1>{c.id}</h1>
  ) 
})) 
}
})}
Sign up to request clarification or add additional context in comments.

Comments

1

Don't open another ( bracket after arrow function just go like this

const mobiletasks = project_tasks.map((project_task, index) =>

 <mobileItems key={project_task.viewproducts3.id}

project_task={project_task} /> );

Comments

1

Try this.

let mobile = [];

const mobiletasks = Object.values(project_tasks).map((project_task, index) => (
<mobileItems key={index} project_task={project_task} />
));  

mobile.push(mobiletasks);

Comments

0

Look at your redux state: project_task has this structure:

project_task: {
    project_tasks: {
        viewproducts3: [],
        viewproducts1: [],
        viewproducts2: [],
        categories: [],
        project_task: {}
    }
}

The map method is included in the Array.prototype (see map doc), so you have to use it with arrays instead of with objects (project_tasks is an object).

Comments

0

Basically you are trying to map an object which is wrong,

object image

THEORETICAL SOLUTION

project_tasks have 4 objects inside and if you are trying to map the data from viewproducts1-2-3, you need to push it to a variable like this

let tasks = []
tasks.push(...project_tasks.viewproducts1)
tasks.push(...project_tasks.viewproducts2)
tasks.push(...project_tasks.viewproducts3)

this will load every data from viewproducts1-3 to task

const mobiletasks = tasks.map((project_task, index) => (
<mobileItems key={project_task.id} project_task={project_task} />
)); 

and you can happily map the values :)

Hope this help

1 Comment

this dosent work. I am getting error "TypeError: Invalid attempt to spread non-iterable instance".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.