-2

I have an array of nested objects.

const arr = [
{
  id: 1,
  name: "java",
  answers: {
    q1: "ex",
    q2: "ex",
    q3: "ex",
  },
},
{
  id: 2,
  name: "css",
  answers: {
    q1: "ex",
    q2: "ex",
    q3: "ex",
  },
},];

For example : I want to access and diplay "q1" from the first "answer" object . How can I do that?

Kvorin
  • 7
  • 2

1 Answers1

0

You can reference array items using their numerical index, you want arr[0].answers.q1

const arr = [
{
  id: 1,
  name: "java",
  answers: {
    q1: "ex",
    q2: "ex",
    q3: "ex",
  },
},
{
  id: 2,
  name: "css",
  answers: {
    q1: "ex",
    q2: "ex",
    q3: "ex",
  },
},];

const example = document.getElementById('example');
example.innerHTML = arr[0].answers.q1;
<div id="example"></div>
Pete
  • 4,542
  • 9
  • 43
  • 76