0

How do I access the following keys within this array of objects using the 'map' method? I want to access:

  1. 'id' inside 'moreInfo'
  2. 'fore' inside 'distances'

I get undefined for the following fields when I use

a.map((d) => console.log(d.moreInfo.id));

. I want to use the 'map' method itself so that I can render this content in my React application.

let a = [
  {
    "Id": "huih23432",
    "name": "Kevin",
    "dob": "26/06/1995",
    "nid": "34535353543",
    "images": {
      "id": "https://picsum.photos/200",
      "nid": "https://picsum.photos/200"
    }
  },
  {
    "moreInfo": [
      {
        "id": "243423423423",
        "dob": "16/07/1990",
        "name": "JD",
        "images": {
          "id": "https://picsum.photos/200",
          "nid": "https://picsum.photos/200"
        },
        "distances": {
          "fore": "0.91",
          "towards": "0.5"
        }
      }
    ]
  }
];
3
  • There are two objects in the array, the first one doesn't have moreInfo, the second one does have it. Commented Jul 20, 2020 at 10:58
  • Your object is probably wrong, moreInfo should be inside the first object, not as a second array element. Commented Jul 20, 2020 at 11:00
  • @ritaj this is how I am getting the object. If you can re-structure this, can you tell me? I add in the same in my mock server Commented Jul 20, 2020 at 11:01

2 Answers 2

1

Try this.

    let a = [
        {
                  "Id": "huih23432",
                  "name": "Kevin",
                  "dob": "26/06/1995",
                  "nid": "34535353543",
                  "images": {
                    "id": "https://picsum.photos/200",
                    "nid": "https://picsum.photos/200"
                  }
                },
                {
                  "moreInfo": [
                    {
                      "id": "243423423423",
                      "dob": "16/07/1990",
                      "name": "JD",
                      "images": {
                        "id": "https://picsum.photos/200",
                        "nid": "https://picsum.photos/200"
                      },
                      "distances": {
                        "fore": "0.91",
                        "towards": "0.5"
                      }
                    }
                  ]
                }
        ];
        


  a.filter(d => d.moreInfo)
    .map((d)=>{
      const moreInfoObj =  d.moreInfo.find(y => y.id);
      console.log("'id' inside 'moreInfo': " + moreInfoObj.id);
      console.log("'fore' inside 'distances': " + moreInfoObj.distances.fore);
  });

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

Comments

0

Actually you data array of objects and each object has different props. You can use destructuring with default values.

let a = [
  {
    Id: "huih23432",
    name: "Kevin",
    dob: "26/06/1995",
    nid: "34535353543",
    images: {
      id: "https://picsum.photos/200",
      nid: "https://picsum.photos/200",
    },
  },
  {
    moreInfo: [
      {
        id: "243423423423",
        dob: "16/07/1990",
        name: "JD",
        images: {
          id: "https://picsum.photos/200",
          nid: "https://picsum.photos/200",
        },
        distances: {
          fore: "0.91",
          towards: "0.5",
        },
      },
    ],
  },
];

a.map(({ moreInfo: [{ id, distances: { fore } = {} }] = [{}] }) =>
  console.log({ id, fore })
);

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.