0

This is my array, I'm working on react app

const categoryObj = [
    {
        id: "463e989a-c4f2-4616-85c5-0cb610c5fff0",
        name: "Women",
        subCategory: [
            {
                id: "91ba7308-b68e-4d0c-85d8-0cc8272c6bc8",
                name: "All",
                icon: "AllIcon"
            },
            {
                id: "0e0712c5-0b5a-4d4e-acf5-3d7faf2caa2a",
                name: "Clothes",
                icon: "ClothesIcon",
                sections: [
                    {
                        id: "9b7a7a58-04a1-4aba-ba68-0e6b1390021e",
                        name: "All",
                        href: "Women/Clothes/All".split(' ').join('-').trim().toLowerCase()
                    }
                ]
            }
        ]
    }
]

how can I access "women" in this array ? (I have many items like "women" this is just a sample of a large array. if you can suggest a mapping method it's better.)

1
  • 1
    categoryObj[0].name Commented Jun 10, 2022 at 6:04

4 Answers 4

2

You can map through it to get the name value in this way:

categoryObj.map((category) => category.name)
Sign up to request clarification or add additional context in comments.

Comments

1

const categoryObj = [
    {
        id: "463e989a-c4f2-4616-85c5-0cb610c5fff0",
        name: "Women",
        subCategory: [
            {
                id: "91ba7308-b68e-4d0c-85d8-0cc8272c6bc8",
                name: "All",
                icon: "AllIcon"
            },
            {
                id: "0e0712c5-0b5a-4d4e-acf5-3d7faf2caa2a",
                name: "Clothes",
                icon: "ClothesIcon",
                sections: [
                    {
                        id: "9b7a7a58-04a1-4aba-ba68-0e6b1390021e",
                        name: "All",
                        href: "Women/Clothes/All".split(' ').join('-').trim().toLowerCase()
                    }
                ]
            }
        ]
    },
    {
        id: "463e989a-c4f2-4616-85c5-0cb610c5fff0",
        name: "Men",
        subCategory: [
            {
                id: "91ba7308-b68e-4d0c-85d8-0cc8272c6bc8",
                name: "All",
                icon: "AllIcon"
            },
            {
                id: "0e0712c5-0b5a-4d4e-acf5-3d7faf2caa2a",
                name: "Clothes",
                icon: "ClothesIcon",
                sections: [
                    {
                        id: "9b7a7a58-04a1-4aba-ba68-0e6b1390021e",
                        name: "All",
                        href: "Women/Clothes/All".split(' ').join('-').trim().toLowerCase()
                    }
                ]
            }
        ]
    },
    {
        id: "463e989a-c4f2-4616-85c5-0cb610c5fff0",
        name: "Children",
        subCategory: [
            {
                id: "91ba7308-b68e-4d0c-85d8-0cc8272c6bc8",
                name: "All",
                icon: "AllIcon"
            },
            {
                id: "0e0712c5-0b5a-4d4e-acf5-3d7faf2caa2a",
                name: "Clothes",
                icon: "ClothesIcon",
                sections: [
                    {
                        id: "9b7a7a58-04a1-4aba-ba68-0e6b1390021e",
                        name: "All",
                        href: "Women/Clothes/All".split(' ').join('-').trim().toLowerCase()
                    }
                ]
            }
        ]
    }
]
const result = categoryObj.filter(obj => obj.name === 'Women')
console.log('multi objects :', result)
// if unique with name 
const resultUnique = categoryObj.find(obj => obj.name === 'Women')
console.log('unique object :', resultUnique)

Comments

0

This object contains an object and have another variations as sub. If you want to access first name,

categoryObj.map((it) => {
   //You can handle it like this
   console.log(it.name)
})

But if you want all name of all subCategory,

categoryObj[0].subCategory.map((cat) => {
   //You can handle it like this
   console.log(cat.name)
})

You have same object-type in and out.

Comments

0
const res  =  categoryObj.filter(item=>item.name === 'Women');

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.