0

this is my code to call arrays of object

 {data.map((item, index ) =>
        <View>
        <Text key={index}>{item.type}</Text>

        {item.resultlist((sub,index)=>

            <Text key={index}>{sub.name}</Text>

          )}
            </View>
    )}

and this is my response of json array

 {
"status": "success",
"message": "Home page Response",
"response": [
    {
        "type": "product",
        "status": true,
        "sort_order": 0,
        "resultlist": [
            {
                "name": "Mifa F1",
                "img": "https://www.achhacart.com/image/cache/catalog/new%20thumbnails/Mifa%20A1BlacjkThumbnail-600x600.jpg",
                "type": "product",
                "product_id": 87
            },
            {
                "name": "Earphone",
                "img": "https://www.achhacart.com/image/catalog/cmsblock/hgb5.png",
                "type": "category",
                "category_id": 20
            },
            {
                "name": "Air Purifier",
                "img": "https://www.achhacart.com/image/catalog/cmsblock/air.gif",
                "type": "product",
                "product_id": 87
            },
            {
                "name": "Powerbank",
                "img": "https://www.achhacart.com/image/catalog/cmsblock/Powerbank10.jpg",
                "type": "product",
                "product_id": 87
            }
        ]
    },
    {
        "type": "middleimage",
        "status": true,
        "sort_order": 1,
        "img": "https://www.achhacart.com/image/catalog/cmsblock/Powerbank10.jpg",
        "product_id": 187
    },
    {
        "type": "product",
        "status": true,
        "sort_order": 2,
        "resultlist": [
            {
                "name": "Mifa A1 Black",
                "img": "https://www.achhacart.com/image/cache/catalog/new%20thumbnails/Mifa%20A1BlacjkThumbnail-600x600.jpg",
                "type": "product",
                "product_id": 87
            },
            {
                "name": "Earphones",
                "img": "https://www.achhacart.com/image/catalog/cmsblock/hgb5.png",
                "type": "category",
                "category_id": 20
            },
            {
                "name": "Air Purifiers",
                "img": "https://www.achhacart.com/image/catalog/cmsblock/air.gif",
                "type": "product",
                "product_id": 87
            },
            {
                "name": "Powerbanks",
                "img": "https://www.achhacart.com/image/catalog/cmsblock/Powerbank10.jpg",
                "type": "product",
                "product_id": 87
            }
        ]
    },
    {
        "type": "slider",
        "status": true,
        "sort_order": 3,
        "resultlist": [
            {
                "title": "slider1",
                "link": "",
                "image": "https://www.achhamall.com/staging-achhamall.com/image/catalog/1AA/WeChatImage_20191228151402.jpg"
            },
            {
                "title": "slider2",
                "link": "",
                "image": "https://www.achhamall.com/staging-achhamall.com/image/catalog/1accc/WeChatImage_20191231125513.jpg"
            }
        ]
    }
]

}

how should i call the sub array inside the whole array function in react native i am using the map function but still the error is same and when i call the array object outside the array object then its render suggest me where i am wrong

4
  • 1
    i am storing my array data in state function const data = this.state.achhamall; at globally Commented Jan 15, 2020 at 12:00
  • 1
    item.resultlist should probably be changed to item.resultlist.map Commented Jan 15, 2020 at 12:03
  • 1
    now it give type-error undefined is not an object (evaluating 'item.resultlist.map') Commented Jan 15, 2020 at 12:06
  • 1
    Well yes, not all your items have the object resultList on them Commented Jan 15, 2020 at 12:08

2 Answers 2

2

You are missing map by the looks of it when you are trying to iterate over resultlist.

item.resultlist -> item.resultlist.map((sub, index) => { ... }

It also looks like not every item in data contains a resultlist so maybe check if it exists also.

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

1 Comment

@AoudeshJaiswal how did you do the check?
1

There are two things 1) First, Yoo have to apply map in your child array also 2) You are not returning the tag elements.

Use below code

{data.map((item, index ) =>
    return (
        <View>
            <Text key={index}>{item.type}</Text>
            {item.resultlist.map((sub,index)=>
       return (
                    <Text key={index}>{sub.name}</Text>
                )
            )}
        </View>
    );
)}

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.