0

I am trying to loop through the following array but did not succeed,

is my approach correct? I have done many changes to my code it might be not as it was

my try :

var gender = ["men", "women"];
var category = [
    "topwear",
    "bottomwear",
    "indianwear",
    "westernwear",
];

for (var i = 0; i < categories.length; i++) {
    for (var g = 0; g < gender.length; g++) {
        for (j = 0; j < categories[i][gender[g]].length; j++) {
            for (var c = 0; c < category.length; c++) {
                console.log("=======category========", category[c]);
                for (k = 0; k < categories[i][gender[g]][j][category[c]].length; k++) {
                    console.log(categories[i][gender[g]][j][category[c]][k]);
                }
            }
        }
    }
}

the array :

 [
    {
        men: [
            {
                topwear: [
                    "men-topwear",
                    "men-tshirts",
                    "men-casual-shirts",
                    "men-formal-shirts",
                    "men-sweatshirts",
                    "men-sweaters",
                    "men-jackets",
                    "men-blazers",
                    "men-suits",
                    "rain-jacket",
                ],
            },
            {
                bottomwear: [
                    "men-jeans",
                    "men-casual-trousers",
                    "men-formal-trousers",
                    "mens-shorts",
                    "men-trackpants",
                    "men-innerwear",
                    "men-briefs-and-trunks",
                    "men-boxers",
                    "men-innerwear-vests",
                    "men-nightwear",
                    "men-thermals",
                ],
            },
        ],
    },
    {
        women: [
            {
                indianwear: [
                    "women-kurtas-kurtis-suits",
                    "ethnic-tops",
                    "ethnic-wear-dresses-menu",
                    "women-ethnic-bottomwear?f=categories%3AChuridar%2CLeggings%2CSalwar",
                    "skirts-palazzos",
                    "saree",
                    "dress-material",
                    "lehenga-choli",
                    "dupatta-shawl",
                    "women-ethnic-wear-jackets",
                    "dresses?f=Gender%3Amen%20women%2Cwomen",
                    "jumpsuits?f=Gender%3Amen%20women%2Cwomen",
                ],
            },
            {
                westernwear: [
                    "women-shirts-tops-tees",
                    "women-jeans-jeggings",
                    "women-trousers",
                    "women-shorts-skirts",
                    "women-shrugs",
                    "women-sweaters-sweatshirts",
                    "women-jackets-coats",
                    "women-blazers-waistcoats",
                ],
            },
        ],
    },
];

I need to get the names within the array. coz i need to use it in the code which i am going to write inside the inner most loop.

thanks in advance

2
  • 2
    men: and women: should be in the same object, not different objects. Commented Oct 12, 2020 at 18:22
  • ohh. can u pls provide solution ? Commented Oct 12, 2020 at 18:24

1 Answer 1

1

Your code expects each object in the array to have both men and women properties. But these properties are in separate array elements. So you need to check whether the property exists before trying to loop over its elements. Otherwise you'll try to get the length of an undefined property.

Similarly, each object in the men or women array has just a single property, not all the properties in the category array.

Looping will be much easier if you assign variables to the current element of the array, so you don't need long expressions with lots of indexes like categories[i][gender[g]][j][category[c]][k]. And using forEach() will simplify this.

var gender = ["men", "women"];
var category = [
  "topwear",
  "bottomwear",
  "indianwear",
  "westernwear",
];

var categories = [{
    men: [{
        topwear: [
          "men-topwear",
          "men-tshirts",
          "men-casual-shirts",
          "men-formal-shirts",
          "men-sweatshirts",
          "men-sweaters",
          "men-jackets",
          "men-blazers",
          "men-suits",
          "rain-jacket",
        ],
      },
      {
        bottomwear: [
          "men-jeans",
          "men-casual-trousers",
          "men-formal-trousers",
          "mens-shorts",
          "men-trackpants",
          "men-innerwear",
          "men-briefs-and-trunks",
          "men-boxers",
          "men-innerwear-vests",
          "men-nightwear",
          "men-thermals",
        ],
      },
    ],
  },
  {
    women: [{
        indianwear: [
          "women-kurtas-kurtis-suits",
          "ethnic-tops",
          "ethnic-wear-dresses-menu",
          "women-ethnic-bottomwear?f=categories%3AChuridar%2CLeggings%2CSalwar",
          "skirts-palazzos",
          "saree",
          "dress-material",
          "lehenga-choli",
          "dupatta-shawl",
          "women-ethnic-wear-jackets",
          "dresses?f=Gender%3Amen%20women%2Cwomen",
          "jumpsuits?f=Gender%3Amen%20women%2Cwomen",
        ],
      },
      {
        westernwear: [
          "women-shirts-tops-tees",
          "women-jeans-jeggings",
          "women-trousers",
          "women-shorts-skirts",
          "women-shrugs",
          "women-sweaters-sweatshirts",
          "women-jackets-coats",
          "women-blazers-waistcoats",
        ],
      },
    ],
  },
];

categories.forEach(catItem =>
  gender.forEach(genItem => {
    if (catItem[genItem]) {
      category.forEach(cat => {
        console.log("=======category========", cat);
        catItem[genItem].forEach(i => {
          if (i[cat]) {
            i[cat].forEach(x => console.log(x));
          }
        });
      });
    }
  })
);

Sign up to request clarification or add additional context in comments.

1 Comment

its showing me for (k = 0; k < categories[i][gender[g]][j][category[c]].length; k++ TypeError: Cannot read property 'length' of undefined

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.