0

i wanted to loop through the array inside on an object and pull the data to render in the react component. but i don't know how to use map to the array inside on the object. i tried many way's but it's not working

const [emailData, setEmailData] = useState("");

res?.data.emails.map((email) =>  
    setEmailData(
        <Table.Row>
            <Table.Cell>{email.emailed_to}</Table.Cell>
            <Table.Cell>{email.notes}</Table.Cell>
            <Table.Cell>{email.created_at}</Table.Cell>
        </Table.Row>
    );
);

return ({ emailData });

1 Answer 1

1

You can use the Object functions for that which wrap your object.

const object1 = {a: 1, b: 2, c: 3}

There are 3 versions of it:

  • Keys: Object.keys(object1) returns an array of all keys => [a,b,c]
  • Values: Object.values(object1) returns an array of all values => [1,2,3]
  • Entries: Object.entries(object1) returns an array of all entires as an array of arrays=> [[a,1],[b,2],[c,3]]

The last one could be used like this

Object.entries(object1).map(([key, value]) => ....)
Sign up to request clarification or add additional context in comments.

1 Comment

@Shuvo Perfect, if this helped you, could you mark it as accepted so other might find it as well?

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.