0

I have an array that's something like this:

{
"data": [
    {
        "id": 4,
        "attributes": {
            "Title": "Teste",
            "Content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
            "createdAt": "2022-07-05T01:38:32.442Z",
            "updatedAt": "2022-07-05T01:42:10.937Z",
            "publishedAt": "2022-07-05T01:38:32.922Z",
            "locale": "pt-BR",
            "categoria": {
                "data": {
                    "id": 6,
                    "attributes": {
                        "Nome": "Web Design",
                        "createdAt": "2022-07-05T01:38:14.887Z",
                        "updatedAt": "2022-07-05T01:38:15.416Z",
                        "publishedAt": "2022-07-05T01:38:15.415Z",
                        "locale": "pt-BR"
                    }
                }
            },
            "autor": {
                "data": {
                    "id": 1,
                    "attributes": {
                        "Nome": "Talles",
                        "createdAt": "2022-07-05T01:17:27.766Z",
                        "updatedAt": "2022-07-05T01:17:27.766Z"
                    }
                }
            }
        }
    },
    {
        "id": 5,
        ...

I want to create a loop to data[0 or 1 or 2 or whatever].attributes.Title

How can I do it without knowing the index from data?

  • Do you just want an array of titles? What have you attempted? Please add that to your question as a [mcve]. – Andy Jul 05 '22 at 17:06
  • What's the problem. `object.data.forEach(item => console.log(item.attributes))` – Barmar Jul 05 '22 at 17:07

1 Answers1

0

data.forEach(val => console.log(val.attributes)); Here, I have used forEach loop and just access attributes with val.attributes.

let data = [{
  "id": 4,
  "attributes": {
    "Title": "Teste",
    "Content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
    "createdAt": "2022-07-05T01:38:32.442Z",
    "updatedAt": "2022-07-05T01:42:10.937Z",
    "publishedAt": "2022-07-05T01:38:32.922Z",
    "locale": "pt-BR",
    "categoria": {
      "data": {
        "id": 6,
        "attributes": {
          "Nome": "Web Design",
          "createdAt": "2022-07-05T01:38:14.887Z",
          "updatedAt": "2022-07-05T01:38:15.416Z",
          "publishedAt": "2022-07-05T01:38:15.415Z",
          "locale": "pt-BR"
        }
      }
    },
    "autor": {
      "data": {
        "id": 1,
        "attributes": {
          "Nome": "Talles",
          "createdAt": "2022-07-05T01:17:27.766Z",
          "updatedAt": "2022-07-05T01:17:27.766Z"
        }
      }
    }
  }
}]
data.forEach(val => console.log(val.attributes));
Nexo
  • 2,125
  • 2
  • 10
  • 20
  • 1
    Adding to this you could use `var attributeArray = data.map(val => console.log(val.attributes));` instead to be able to access the data as an array – Devon Ray Jul 05 '22 at 17:12