Get first item from each object in a JSON response

1
  • I'm getting a json through the following URL: link (via GET)

  • I'm iterating over objects, but I wanted to get only the first item of each object, which in this case would be URL.

  • How do I do this?

    axios.get("http://jsonplaceholder.typicode.com/photos").then((response) => {
        var data = response.data;
        for(i in data){
            console.log(response.data[i])
        }
    });
    
asked by anonymous 24.12.2018 / 22:25

2 answers

2

You have already passed the response to variable data. You can do this by checking if the variable is different from the value of albumId :

var data = [
  {
    "albumId": 1,
    "id": 1,
    "title": "accusamus beatae ad facilis cum similique qui sunt",
    "url": "https://via.placeholder.com/600/92c952",
    "thumbnailUrl": "https://via.placeholder.com/150/92c952"
  },
  {
    "albumId": 1,
    "id": 2,
    "title": "reprehenderit est deserunt velit ipsam",
    "url": "https://via.placeholder.com/600/771796",
    "thumbnailUrl": "https://via.placeholder.com/150/771796"
  },
  {
    "albumId": 1,
    "id": 3,
    "title": "officia porro iure quia iusto qui ipsa ut modi",
    "url": "https://via.placeholder.com/600/24f355",
    "thumbnailUrl": "https://via.placeholder.com/150/24f355"
  },
  {
    "albumId": 2,
    "id": 51,
    "title": "non sunt voluptatem placeat consequuntur rem incidunt",
    "url": "https://via.placeholder.com/600/8e973b",
    "thumbnailUrl": "https://via.placeholder.com/150/8e973b"
  },
  {
    "albumId": 2,
    "id": 52,
    "title": "eveniet pariatur quia nobis reiciendis laboriosam ea",
    "url": "https://via.placeholder.com/600/121fa4",
    "thumbnailUrl": "https://via.placeholder.com/150/121fa4"
  },
];

// variável de controle para não pegar o mesmo id
var albumId;

for(let i in data){
   // atribui o valor de albumId a variável id
   let id = data[i].albumId;
   // se for diferente, pega o valor da url
   if(albumId != id){
      // redefine o valor da variável com o valor atual
      albumId = id;
      console.log(data[i].url);
   }
}

Suddenly you can create an array and then add the urls and then do whatever you want with them:

var data = [
  {
    "albumId": 1,
    "id": 1,
    "title": "accusamus beatae ad facilis cum similique qui sunt",
    "url": "https://via.placeholder.com/600/92c952",
    "thumbnailUrl": "https://via.placeholder.com/150/92c952"
  },
  {
    "albumId": 1,
    "id": 2,
    "title": "reprehenderit est deserunt velit ipsam",
    "url": "https://via.placeholder.com/600/771796",
    "thumbnailUrl": "https://via.placeholder.com/150/771796"
  },
  {
    "albumId": 1,
    "id": 3,
    "title": "officia porro iure quia iusto qui ipsa ut modi",
    "url": "https://via.placeholder.com/600/24f355",
    "thumbnailUrl": "https://via.placeholder.com/150/24f355"
  },
  {
    "albumId": 2,
    "id": 51,
    "title": "non sunt voluptatem placeat consequuntur rem incidunt",
    "url": "https://via.placeholder.com/600/8e973b",
    "thumbnailUrl": "https://via.placeholder.com/150/8e973b"
  },
  {
    "albumId": 2,
    "id": 52,
    "title": "eveniet pariatur quia nobis reiciendis laboriosam ea",
    "url": "https://via.placeholder.com/600/121fa4",
    "thumbnailUrl": "https://via.placeholder.com/150/121fa4"
  },
];

// cria a array
var urls = [];

// variável de controle para não pegar o mesmo id
var albumId;

for(let i in data){
   // atribui o valor de albumId a variável id
   let id = data[i].albumId;
   // se for diferente, pega o valor da url
   if(albumId != id){
      // redefine o valor da variável com o valor atual
      albumId = id;
      // adiciona as urls à array
      urls.push(data[i].url);
   }
}

// percorre a array
for(let url of urls){
   console.log(url);
}
    
24.12.2018 / 22:59
2

      var data = [
      {
        "albumId": 1,
        "id": 1,
        "title": "accusamus beatae ad facilis cum similique qui sunt",
        "url": "https://via.placeholder.com/600/92c952",
        "thumbnailUrl": "https://via.placeholder.com/150/92c952"
      },
      {
        "albumId": 1,
        "id": 2,
        "title": "reprehenderit est deserunt velit ipsam",
        "url": "https://via.placeholder.com/600/771796",
        "thumbnailUrl": "https://via.placeholder.com/150/771796"
      },
      {
        "albumId": 1,
        "id": 3,
        "title": "officia porro iure quia iusto qui ipsa ut modi",
        "url": "https://via.placeholder.com/600/24f355",
        "thumbnailUrl": "https://via.placeholder.com/150/24f355"
      },
      {
        "albumId": 2,
        "id": 51,
        "title": "non sunt voluptatem placeat consequuntur rem incidunt",
        "url": "https://via.placeholder.com/600/8e973b",
        "thumbnailUrl": "https://via.placeholder.com/150/8e973b"
      },
      {
        "albumId": 2,
        "id": 52,
        "title": "eveniet pariatur quia nobis reiciendis laboriosam ea",
        "url": "https://via.placeholder.com/600/121fa4",
        "thumbnailUrl": "https://via.placeholder.com/150/121fa4"
      },
    ];

    var aux = ""
    
    for(i in data){
        if (data[i].albumId != aux){
            console.log(data[i].url)
            aux = data[i].albumId
        }
        
    }

I believe it would be something like:

where url can be any JSON attribute:

  • albumId

  • id

  • title

  • url

  • thumbnailUrl

24.12.2018 / 22:32