Get first item of each object in a JSON response

Asked

Viewed 779 times

1

  • I am getting a json through the following URL: http://jsonplaceholder.typicode.com/photos (via get)

  • I’m iterating on the objects, but I wanted to get just the first one item of each object, in which case it would be URL.

  • How do I do that?

    axios.get("http://jsonplaceholder.typicode.com/photos").then((response) => {
        var data = response.data;
        for(i in data){
            console.log(response.data[i])
        }
    });
    
  • I was just catching up, buddy.

2 answers

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

  • In case I wanted to get the first url of each albumId. For example: there are 5 photos with the same albumId. I want to get only the first url of these photos.

  • for example in Id 1 https://via.placeholder.com/600/92c952 and 2 https://via.placeholder.com/600/8e973b ???

  • 1

    That’s exactly what it is.

  • Take the first photo of each album

  • forehead I think putting this if already solve your problem

  • Qual if, amigo?

  • That one : if (data[i].albumId != aux)

  • Aux refers to what?

  • I think your browser is caching the page tries to reload with Ctrl + F5 and view my edited response.

  • I couldn’t get :/

  • I don’t quite understand this whole aux thing

  • This is a proper javascript function?

  • is an auxiliary variable, for good practices wrote aux to identify her better.

Show 8 more comments

2


You have already passed the answer to the date variable. You can do so by checking if the variable is different from the 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 go adding 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);
}

  • Exactly friend. However, I need to get the first url of each albumId. Ex: When the albumId is 1, I take the first url and jump to albumId 2.

  • Now I’ve drawn...

  • Thank you so much for the help, perfect your explanation!

  • the cool thing is that the guy did the same logic as mine

  • @Gunblades hadn’t even seen his answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.