0

I want to achieve the data rendering in a form of table something like this:

enter image description here

I have a data (stored in variable 'myData') of form:

{
 "id":0,
 "name": ["wheat", "Rice", "Sugar"],
 "Quality":["Good", "good","good"],
 "Quantity":["200","200","200"]
}

I tried mapping the and together to implement this, but it renders the data horizontally in a single row.

This is what I tried to achieve so far with minimum test code.

return (
            <View style={{flexDirection:'column'}}>{myData.map( (item) => {
                return (
                    <View key={item.id}>
                        <View>
                            <Text>
                                {item.name}
                            </Text>
                        </View>
                        <View>
                            <Text >
                                {item.Quality}
                            </Text>
                        </View>
                    </View>

                )
            })}

            </View>
        )

Please help to achieve this

2 Answers 2

2

map each array into it's own View and use flexDirection: "row"

const list = myData.map(item => {
  return (
    <View style={{ flexDirection: 'row'}}>
      <View style={{ flex: 1}}>
        {item.name.map((name, i) => (
          <Text>{i + 1}</Text>
        ))}
      </View>
      <View style={{ flex: 1}}>
        {item.name.map((name, i) => (
          <Text>{name}</Text>
        ))}
      </View>
      <View style={{ flex: 1}}>
        {item.Quality.map((quality, i) => (
          <Text>{quality}</Text>
        ))}
      </View>
      <View style={{ flex: 1}}>
        {item.Quantity.map((quantity, i) => (
          <Text>{`${quantity} Bags`}</Text>
        ))}
      </View>
    </View>
  );
});
Sign up to request clarification or add additional context in comments.

3 Comments

This worked. Can you please explain this? Why we needed to follow such approach? Where I Was lacking?
because you have three arrays and to display data you need to loop through each array. flowDirection: 'row' will display the items as rows.
Hey, @Junius Can check this question, please!! stackoverflow.com/questions/57595796/…
0
Try this,

    return (
                <View style={{flexDirection:'column'}}>{myData.name.map( (item, index) => {
                    return (
                        <View key={item.id}>
                            <View>
                                <Text>
                                    {item}
                                </Text>
                            </View>
                            <View>
                                <Text >
                                    {myData.Quality[index]}
                                </Text>
                            </View>
 <View>
                                <Text >
                                    {myData.Quantity[index]}
                                </Text>
                            </View>
                        </View>

                    )
                })}

                </View>
            )

3 Comments

I tired this but not working. What is the need for 'myData.name.map()' when my array is aready starting without any key object name
I think myData is your dictionary. If so, I first pick up your array 'name' and try to map.
I edited my question. myData is the variable name in which i am storing my data /array

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.