0

I am trying to mapping through three objects in react to render multiple categories on react template , Code doesn't give any error but its not showing any content on react web page.

return (

    <div className="container pt-80">
        <center>Category Grouping</center>
        {categories.map((cate,key)=>{

            subCategories.map(subCate=>{

                if(subCate.category === cate.id){

                    pType.map(ptype=>{

                        if (ptype.category === subCate.id){

                            console.log("Category : ",cate.category)
                            console.log("Sub Category : ",subCate.subCatName)
                            console.log("Product Type : ",ptype.ptype)
                            
                            console.log("*******************************************")
                            
                            return(
                                <Fragment>
                                    <h1 style={{marginLeft:"30px"}}>{cate.category}</h1>
                                    <h1 style={{marginLeft:"60px"}}>{subCate.subCatName}</h1>
                                    <h1 style={{marginLeft:"120px"}}>{ptype.ptype}</h1>
                                </Fragment>
                            )
                        }
                    })
                }
            })
        })}
    </div>
    
)

Its printing the correct values in console :

enter image description here

1
  • 2
    Maybe you are missing a return before subCategories.map. Also Return null for else condition and wrap the return from subCategories.map in a div and finally don't miss the key prop Commented Aug 20, 2021 at 17:31

2 Answers 2

2

Extending what @Akhil said in the comment. You are actually not returning anything in you're first two map calls, only the last.

add return before both nested map calls:

return subCategories.map(subCate=>{...

and

return pType.map(ptype=>{

Also I would add a return null after your if statements. Map expects a return value.

if(subCate.category === cate.id){
 ....
}
return null;

and

if (ptype.category === subCate.id){
 ....
}
return null;
Sign up to request clarification or add additional context in comments.

Comments

1

Look into the comment by @Akhil. You missed the return for the map.

const categories = [{ id: 1, category: "Foods & Supplements" }];
const subCategories = [{ id: 1, category: 1, subCatName: "Herbal Drinks" }];
const pType = [
  { id: 1, category: 1, ptype: "Herbal Juice" },
  { id: 2, category: 1, ptype: "Herbal Coffee Tea&Soup" }
];
export default function App() {
  return (
    <div>
      <h1>Category Grouping</h1>
      {categories.map((cate, key) => (
        <div key={key}>
          {subCategories.map((subCate, sKey) => (
            <div key={sKey}>
              {subCate.category === cate.id &&
                pType.map((ptype, pKey) => (
                  <div key={pKey}>
                    {ptype.category === subCate.id && (
                      <>
                        <h1 style={{ marginLeft: "30px" }}>{cate.category}</h1>
                        <h1 style={{ marginLeft: "60px" }}>
                          {subCate.subCatName}
                        </h1>
                        <h1 style={{ marginLeft: "120px" }}>{ptype.ptype}</h1>
                      </>
                    )}
                  </div>
                ))}
            </div>
          ))}
        </div>
      ))}
    </div>
  );
}

Also, use some sort of linting (e.g. Eslint) and format the code, both will help to catch syntax errors.

Comments

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.