Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Render all properties in an object which is in an array which is in an object

tdev81

New Coder
I think this probably has a simple solution but I've been trying for hours and I can't figure it out. I have an array with objects that have properties:

Code:
contactInfo = {
addresses: [
{
   addressLine1: "32030 Ortiz Fords"
   city: "Port Devon"
   country: "Guadeloupe"
   addressType: "BUSINESS"
},
{
   addressLine1: "333 High Street"
   city: "New York"
   country: "USA"
   addressType: "PERSONAL"
}]
}




I'm trying to output each address in it's own div, like:


Code:
<div>
Business Address:
32030 Ortiz Fords
Port Devon
Guadeloupe
<div/>

<div>
Personal Address:
333 High Street
New York
USA
<div/>


But every solution I come up with ends up outputting BOTH addressLine1's after one another, then both cities after one another, then both countries after one another, like:


Code:
const allAddresses = primaryContact.address.map((address) => ({
    address,
  }));


{allAddresses
    .map((address) => address)
    .map((address) => {
     return (
     <div key={address.address.id}>
    {address.address.addressLine1}
    {address.address.city}
    {address.address.country}
     </div>
     );
})}

I've tried using forEach, like:


Code:
{allPrimaryAddress.map((address) => {address.address}).forEach((address)=> {
                    return (
                      {address.address.addressLine1}
                      {address.address.city}
                      {address.address.city}
                    )
))}

but this won't even output to the DOM because the entire thing shows as an error in VSCode. Also, I'm using typescript but I figured I'd try out this forum. Any one know how to do this?




Render each object from array which is on an object

 
Here is one way.

HTML:
<!DOCTYPE html>
<html lang="en">
    <head>
        <script>
            contactInfo = {
    addresses: [
    {
       addressLine1: "32030 Ortiz Fords",
       city: "Port Devon",
       country: "Guadeloupe",
       addressType: "BUSINESS",
    },
    {
       addressLine1: "333 High Street",
       city: "New York",
       country: "USA",
       addressType: "PERSONAL",
    }]
    }
        </script>

        <style>
            table {
                width: 400px;
                margin-bottom: 12px;
                border: solid 1px black;
                box-shadow: 0 0 3px black;
                border-radius: 10px;
                padding: 1em;
                border-spacing: 0;
            }

            td {
                border-bottom: solid 1px lightgray;
            }
        </style>
    </head>
    <body>
        <script>
            for (let i = 0; i < contactInfo['addresses'].length; i++) {
                document.write("<table>")
                for (data in contactInfo['addresses'][i]) {
                    document.write("<tr><td>" + data + ": </td><td>" + contactInfo['addresses'][i][data] + "</td></tr>")
                    }

                document.write("</table>")
            }
        </script>
    </body>
</html>


1673159030495.png
 
Back
Top Bottom