-1

Estou aprendendo a usar a API do Github e para isso peguei um usuario para usar de exemplo. Estou recebendo uma lista de objetos e preciso selecionar os nomes dos 3 itens mais antigos e deixa-los em ordem crescente (mais antigo para o mais atual).

Este é o meu código(Está em JS):

async function testeApi() {
    const url = "https://api.github.com/search/repositories?q=user:fabpot";
    const response = await fetch(url);
    const result = await response.json();

    //console.log(result);

    result.items.forEach(i=>console.log(i.name));
}

o comando console.log(result); me retorna o seguinte resultado:

{
  total_count: 4,
  incomplete_results: false,
  items: [
    {
      id: 1174001,
      node_id: 'MDEwOlJlcG9zaXRvcnkxMTc0MDAx',
      name: 'sphinx-php',
      full_name: 'fabpot/sphinx-php',
      private: false,
      owner: [Object],
      html_url: 'https://github.com/fabpot/sphinx-php',
      description: 'My Sphinx Extensions',
      fork: false,
      url: 'https://api.github.com/repos/fabpot/sphinx-php',
      forks_url: 'https://api.github.com/repos/fabpot/sphinx-php/forks',
      keys_url: 'https://api.github.com/repos/fabpot/sphinx-php/keys{/key_id}',
      collaborators_url: 'https://api.github.com/repos/fabpot/sphinx-php/collaborators{/collaborator}',
      teams_url: 'https://api.github.com/repos/fabpot/sphinx-php/teams',
      hooks_url: 'https://api.github.com/repos/fabpot/sphinx-php/hooks',
      issue_events_url: 'https://api.github.com/repos/fabpot/sphinx-php/issues/events{/number}',
      events_url: 'https://api.github.com/repos/fabpot/sphinx-php/events',
      assignees_url: 'https://api.github.com/repos/fabpot/sphinx-php/assignees{/user}',
      branches_url: 'https://api.github.com/repos/fabpot/sphinx-php/branches{/branch}',
      tags_url: 'https://api.github.com/repos/fabpot/sphinx-php/tags',
      blobs_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/blobs{/sha}',
      git_tags_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/tags{/sha}',
      git_refs_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/refs{/sha}',
      trees_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/trees{/sha}',
      statuses_url: 'https://api.github.com/repos/fabpot/sphinx-php/statuses/{sha}',
      languages_url: 'https://api.github.com/repos/fabpot/sphinx-php/languages',
      stargazers_url: 'https://api.github.com/repos/fabpot/sphinx-php/stargazers',
      contributors_url: 'https://api.github.com/repos/fabpot/sphinx-php/contributors',
      subscribers_url: 'https://api.github.com/repos/fabpot/sphinx-php/subscribers',
      subscription_url: 'https://api.github.com/repos/fabpot/sphinx-php/subscription',
      commits_url: 'https://api.github.com/repos/fabpot/sphinx-php/commits{/sha}',
      git_commits_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/commits{/sha}',
      comments_url: 'https://api.github.com/repos/fabpot/sphinx-php/comments{/number}',
      issue_comment_url: 'https://api.github.com/repos/fabpot/sphinx-php/issues/comments{/number}',
      contents_url: 'https://api.github.com/repos/fabpot/sphinx-php/contents/{+path}',
      compare_url: 'https://api.github.com/repos/fabpot/sphinx-php/compare/{base}...{head}',
      merges_url: 'https://api.github.com/repos/fabpot/sphinx-php/merges',
      archive_url: 'https://api.github.com/repos/fabpot/sphinx-php/{archive_format}{/ref}',
      downloads_url: 'https://api.github.com/repos/fabpot/sphinx-php/downloads',
      issues_url: 'https://api.github.com/repos/fabpot/sphinx-php/issues{/number}',
      pulls_url: 'https://api.github.com/repos/fabpot/sphinx-php/pulls{/number}',
      milestones_url: 'https://api.github.com/repos/fabpot/sphinx-php/milestones{/number}',
      notifications_url: 'https://api.github.com/repos/fabpot/sphinx-php/notifications{?since,all,participating}',
      labels_url: 'https://api.github.com/repos/fabpot/sphinx-php/labels{/name}',
      releases_url: 'https://api.github.com/repos/fabpot/sphinx-php/releases{/id}',
      deployments_url: 'https://api.github.com/repos/fabpot/sphinx-php/deployments',
      created_at: '2010-12-16T12:32:24Z',
      updated_at: '2020-02-01T13:52:28Z',
      pushed_at: '2018-04-20T11:28:29Z',
      git_url: 'git://github.com/fabpot/sphinx-php.git',
      ssh_url: '[email protected]:fabpot/sphinx-php.git',
      clone_url: 'https://github.com/fabpot/sphinx-php.git',
      svn_url: 'https://github.com/fabpot/sphinx-php',
      homepage: '',
      size: 38,
      stargazers_count: 154,
      watchers_count: 154,
      language: 'Python',
      has_issues: true,
      has_projects: false,
      has_downloads: true,
      has_wiki: false,
      has_pages: false,
      forks_count: 35,
      mirror_url: null,
      archived: false,
      disabled: false,
      open_issues_count: 6,
      license: [Object],
      forks: 35,
      open_issues: 6,
      watchers: 154,
      default_branch: 'master',
      score: 1
    },
    {
      id: 119659470,
      node_id: 'MDEwOlJlcG9zaXRvcnkxMTk2NTk0NzA=',
      name: 'gitter',
      full_name: 'fabpot/gitter',
      private: false,
      owner: [Object],
      html_url: 'https://github.com/fabpot/gitter',
      description: null,
      fork: false,
      url: 'https://api.github.com/repos/fabpot/gitter',
      forks_url: 'https://api.github.com/repos/fabpot/gitter/forks',
      keys_url: 'https://api.github.com/repos/fabpot/gitter/keys{/key_id}',
      collaborators_url: 'https://api.github.com/repos/fabpot/gitter/collaborators{/collaborator}',
      teams_url: 'https://api.github.com/repos/fabpot/gitter/teams',
      hooks_url: 'https://api.github.com/repos/fabpot/gitter/hooks',
      issue_events_url: 'https://api.github.com/repos/fabpot/gitter/issues/events{/number}',
      events_url: 'https://api.github.com/repos/fabpot/gitter/events',
      assignees_url: 'https://api.github.com/repos/fabpot/gitter/assignees{/user}',
      branches_url: 'https://api.github.com/repos/fabpot/gitter/branches{/branch}',
      tags_url: 'https://api.github.com/repos/fabpot/gitter/tags',
      blobs_url: 'https://api.github.com/repos/fabpot/gitter/git/blobs{/sha}',
      git_tags_url: 'https://api.github.com/repos/fabpot/gitter/git/tags{/sha}',
      git_refs_url: 'https://api.github.com/repos/fabpot/gitter/git/refs{/sha}',
      trees_url: 'https://api.github.com/repos/fabpot/gitter/git/trees{/sha}',
      statuses_url: 'https://api.github.com/repos/fabpot/gitter/statuses/{sha}',
      languages_url: 'https://api.github.com/repos/fabpot/gitter/languages',
      stargazers_url: 'https://api.github.com/repos/fabpot/gitter/stargazers',
      contributors_url: 'https://api.github.com/repos/fabpot/gitter/contributors',
      subscribers_url: 'https://api.github.com/repos/fabpot/gitter/subscribers',
      subscription_url: 'https://api.github.com/repos/fabpot/gitter/subscription',
      commits_url: 'https://api.github.com/repos/fabpot/gitter/commits{/sha}',
      git_commits_url: 'https://api.github.com/repos/fabpot/gitter/git/commits{/sha}',
      comments_url: 'https://api.github.com/repos/fabpot/gitter/comments{/number}',
      issue_comment_url: 'https://api.github.com/repos/fabpot/gitter/issues/comments{/number}',
      contents_url: 'https://api.github.com/repos/fabpot/gitter/contents/{+path}',
      compare_url: 'https://api.github.com/repos/fabpot/gitter/compare/{base}...{head}',
      merges_url: 'https://api.github.com/repos/fabpot/gitter/merges',
      archive_url: 'https://api.github.com/repos/fabpot/gitter/{archive_format}{/ref}',
      downloads_url: 'https://api.github.com/repos/fabpot/gitter/downloads',
      issues_url: 'https://api.github.com/repos/fabpot/gitter/issues{/number}',
      pulls_url: 'https://api.github.com/repos/fabpot/gitter/pulls{/number}',
      milestones_url: 'https://api.github.com/repos/fabpot/gitter/milestones{/number}',
      notifications_url: 'https://api.github.com/repos/fabpot/gitter/notifications{?since,all,participating}',
      labels_url: 'https://api.github.com/repos/fabpot/gitter/labels{/name}',
      releases_url: 'https://api.github.com/repos/fabpot/gitter/releases{/id}',
      deployments_url: 'https://api.github.com/repos/fabpot/gitter/deployments',
      created_at: '2018-01-31T08:43:54Z',
      updated_at: '2020-02-04T16:51:46Z',
      pushed_at: '2018-01-31T08:44:49Z',
      git_url: 'git://github.com/fabpot/gitter.git',
      ssh_url: '[email protected]:fabpot/gitter.git',
      clone_url: 'https://github.com/fabpot/gitter.git',
      svn_url: 'https://github.com/fabpot/gitter',
      homepage: null,
      size: 67,
      stargazers_count: 56,
      watchers_count: 56,
      language: 'PHP',
      has_issues: true,
      has_projects: true,
      has_downloads: true,
      has_wiki: true,
      has_pages: false,
      forks_count: 1,
      mirror_url: null,
      archived: false,
      disabled: false,
      open_issues_count: 0,
      license: [Object],
      forks: 1,
      open_issues: 0,
      watchers: 56,
      default_branch: 'master',
      score: 1
    },
    {
      id: 185822298,
      node_id: 'MDEwOlJlcG9zaXRvcnkxODU4MjIyOTg=',
      name: 'test-releases',
      full_name: 'fabpot/test-releases',
      private: false,
      owner: [Object],
      html_url: 'https://github.com/fabpot/test-releases',
      description: null,
      fork: false,
      url: 'https://api.github.com/repos/fabpot/test-releases',
      forks_url: 'https://api.github.com/repos/fabpot/test-releases/forks',
      keys_url: 'https://api.github.com/repos/fabpot/test-releases/keys{/key_id}',
      collaborators_url: 'https://api.github.com/repos/fabpot/test-releases/collaborators{/collaborator}',
      teams_url: 'https://api.github.com/repos/fabpot/test-releases/teams',
      hooks_url: 'https://api.github.com/repos/fabpot/test-releases/hooks',
      issue_events_url: 'https://api.github.com/repos/fabpot/test-releases/issues/events{/number}',
      events_url: 'https://api.github.com/repos/fabpot/test-releases/events',
      assignees_url: 'https://api.github.com/repos/fabpot/test-releases/assignees{/user}',
      branches_url: 'https://api.github.com/repos/fabpot/test-releases/branches{/branch}',
      tags_url: 'https://api.github.com/repos/fabpot/test-releases/tags',
      blobs_url: 'https://api.github.com/repos/fabpot/test-releases/git/blobs{/sha}',
      git_tags_url: 'https://api.github.com/repos/fabpot/test-releases/git/tags{/sha}',
      git_refs_url: 'https://api.github.com/repos/fabpot/test-releases/git/refs{/sha}',
      trees_url: 'https://api.github.com/repos/fabpot/test-releases/git/trees{/sha}',
      statuses_url: 'https://api.github.com/repos/fabpot/test-releases/statuses/{sha}',
      languages_url: 'https://api.github.com/repos/fabpot/test-releases/languages',
      stargazers_url: 'https://api.github.com/repos/fabpot/test-releases/stargazers',
      contributors_url: 'https://api.github.com/repos/fabpot/test-releases/contributors',
      subscribers_url: 'https://api.github.com/repos/fabpot/test-releases/subscribers',
      subscription_url: 'https://api.github.com/repos/fabpot/test-releases/subscription',
      commits_url: 'https://api.github.com/repos/fabpot/test-releases/commits{/sha}',
      git_commits_url: 'https://api.github.com/repos/fabpot/test-releases/git/commits{/sha}',
      comments_url: 'https://api.github.com/repos/fabpot/test-releases/comments{/number}',
      issue_comment_url: 'https://api.github.com/repos/fabpot/test-releases/issues/comments{/number}',
      contents_url: 'https://api.github.com/repos/fabpot/test-releases/contents/{+path}',
      compare_url: 'https://api.github.com/repos/fabpot/test-releases/compare/{base}...{head}',
      merges_url: 'https://api.github.com/repos/fabpot/test-releases/merges',
      archive_url: 'https://api.github.com/repos/fabpot/test-releases/{archive_format}{/ref}',
      downloads_url: 'https://api.github.com/repos/fabpot/test-releases/downloads',
      issues_url: 'https://api.github.com/repos/fabpot/test-releases/issues{/number}',
      pulls_url: 'https://api.github.com/repos/fabpot/test-releases/pulls{/number}',
      milestones_url: 'https://api.github.com/repos/fabpot/test-releases/milestones{/number}',
      notifications_url: 'https://api.github.com/repos/fabpot/test-releases/notifications{?since,all,participating}',
      labels_url: 'https://api.github.com/repos/fabpot/test-releases/labels{/name}',
      releases_url: 'https://api.github.com/repos/fabpot/test-releases/releases{/id}',
      deployments_url: 'https://api.github.com/repos/fabpot/test-releases/deployments',
      created_at: '2019-05-09T15:08:29Z',
      updated_at: '2019-11-19T09:44:37Z',
      pushed_at: '2019-05-09T15:10:42Z',
      git_url: 'git://github.com/fabpot/test-releases.git',
      ssh_url: '[email protected]:fabpot/test-releases.git',
      clone_url: 'https://github.com/fabpot/test-releases.git',
      svn_url: 'https://github.com/fabpot/test-releases',
      homepage: null,
      size: 3,
      stargazers_count: 2,
      watchers_count: 2,
      language: null,
      has_issues: false,
      has_projects: true,
      has_downloads: true,
      has_wiki: true,
      has_pages: false,
      forks_count: 1,
      mirror_url: null,
      archived: false,
      disabled: false,
      open_issues_count: 0,
      license: null,
      forks: 1,
      open_issues: 0,
      watchers: 2,
      default_branch: 'master',
      score: 1
    },
    {
      id: 237386698,
      node_id: 'MDEwOlJlcG9zaXRvcnkyMzczODY2OTg=',
      name: '.github',
      full_name: 'fabpot/.github',
      private: false,
      owner: [Object],
      html_url: 'https://github.com/fabpot/.github',
      description: null,
      fork: false,
      url: 'https://api.github.com/repos/fabpot/.github',
      forks_url: 'https://api.github.com/repos/fabpot/.github/forks',
      keys_url: 'https://api.github.com/repos/fabpot/.github/keys{/key_id}',
      collaborators_url: 'https://api.github.com/repos/fabpot/.github/collaborators{/collaborator}',
      teams_url: 'https://api.github.com/repos/fabpot/.github/teams',
      hooks_url: 'https://api.github.com/repos/fabpot/.github/hooks',
      issue_events_url: 'https://api.github.com/repos/fabpot/.github/issues/events{/number}',
      events_url: 'https://api.github.com/repos/fabpot/.github/events',
      assignees_url: 'https://api.github.com/repos/fabpot/.github/assignees{/user}',
      branches_url: 'https://api.github.com/repos/fabpot/.github/branches{/branch}',
      tags_url: 'https://api.github.com/repos/fabpot/.github/tags',
      blobs_url: 'https://api.github.com/repos/fabpot/.github/git/blobs{/sha}',
      git_tags_url: 'https://api.github.com/repos/fabpot/.github/git/tags{/sha}',
      git_refs_url: 'https://api.github.com/repos/fabpot/.github/git/refs{/sha}',
      trees_url: 'https://api.github.com/repos/fabpot/.github/git/trees{/sha}',
      statuses_url: 'https://api.github.com/repos/fabpot/.github/statuses/{sha}',
      languages_url: 'https://api.github.com/repos/fabpot/.github/languages',
      stargazers_url: 'https://api.github.com/repos/fabpot/.github/stargazers',
      contributors_url: 'https://api.github.com/repos/fabpot/.github/contributors',
      subscribers_url: 'https://api.github.com/repos/fabpot/.github/subscribers',
      subscription_url: 'https://api.github.com/repos/fabpot/.github/subscription',
      commits_url: 'https://api.github.com/repos/fabpot/.github/commits{/sha}',
      git_commits_url: 'https://api.github.com/repos/fabpot/.github/git/commits{/sha}',
      comments_url: 'https://api.github.com/repos/fabpot/.github/comments{/number}',
      issue_comment_url: 'https://api.github.com/repos/fabpot/.github/issues/comments{/number}',
      contents_url: 'https://api.github.com/repos/fabpot/.github/contents/{+path}',
      compare_url: 'https://api.github.com/repos/fabpot/.github/compare/{base}...{head}',
      merges_url: 'https://api.github.com/repos/fabpot/.github/merges',
      archive_url: 'https://api.github.com/repos/fabpot/.github/{archive_format}{/ref}',
      downloads_url: 'https://api.github.com/repos/fabpot/.github/downloads',
      issues_url: 'https://api.github.com/repos/fabpot/.github/issues{/number}',
      pulls_url: 'https://api.github.com/repos/fabpot/.github/pulls{/number}',
      milestones_url: 'https://api.github.com/repos/fabpot/.github/milestones{/number}',
      notifications_url: 'https://api.github.com/repos/fabpot/.github/notifications{?since,all,participating}',
      labels_url: 'https://api.github.com/repos/fabpot/.github/labels{/name}',
      releases_url: 'https://api.github.com/repos/fabpot/.github/releases{/id}',
      deployments_url: 'https://api.github.com/repos/fabpot/.github/deployments',
      created_at: '2020-01-31T08:10:59Z',
      updated_at: '2020-01-31T08:11:51Z',
      pushed_at: '2020-01-31T08:11:49Z',
      git_url: 'git://github.com/fabpot/.github.git',
      ssh_url: '[email protected]:fabpot/.github.git',
      clone_url: 'https://github.com/fabpot/.github.git',
      svn_url: 'https://github.com/fabpot/.github',
      homepage: null,
      size: 0,
      stargazers_count: 0,
      watchers_count: 0,
      language: null,
      has_issues: true,
      has_projects: true,
      has_downloads: true,
      has_wiki: true,
      has_pages: false,
      forks_count: 0,
      mirror_url: null,
      archived: false,
      disabled: false,
      open_issues_count: 0,
      license: null,
      forks: 0,
      open_issues: 0,
      watchers: 0,
      default_branch: 'master',
      score: 1
    }
  ]
}

Já o comando result.items.forEach(i=>console.log(i.name)); já me retorna so o nome dos repositórios, que fica assim:

sphinx-php
gitter
test-releases
.github

Enfim, os nomes eu já tenho, mas como consigo selecionar os 3 mais antigos e deixa-los em ordem crescente(Do mais antigo ao mais atual)? Me ajudem por favor.

1 Resposta 1

1

Já tendo os dados que deseja, no seu caso a lista de repositórios, pode usar o sort() para ordenar, e o slice() para pegar só os 3 primeiros itens do array.
Veja esse exemplo:

var lista = [
  'sphinx-php',
  'gitter',
  'test-releases',
  '.github',
];

var listaFinal = lista
   .sort()
   .slice(0,3);

console.log(listaFinal);

Veja que o sort está ordenando por ordem crescente. Se precisar uma outra ordem, ou for um objeto com outros atributos, pode passar como parâmetro uma função que ordene, e o slice recebe dois parâmetros, primeiro a posição inicial, segundo (que é opcional) a posição final (não incluso, esse é o limite). Ou seja, pega do elemento na posição zero até o 3, não incluindo esse, que significa pegar do 0 ao 2.

Agora como quer ordenado por data, precisa de uma função que ordene pelo campo correto, por exemplo, "created_at". Veja esse outro exemplo:

var result = {
  total_count: 4,
  incomplete_results: false,
  items: [
    {
      id: 1174001,
      node_id: 'MDEwOlJlcG9zaXRvcnkxMTc0MDAx',
      name: 'sphinx-php',
      full_name: 'fabpot/sphinx-php',
      private: false,
      owner: [Object],
      html_url: 'https://github.com/fabpot/sphinx-php',
      description: 'My Sphinx Extensions',
      fork: false,
      url: 'https://api.github.com/repos/fabpot/sphinx-php',
      forks_url: 'https://api.github.com/repos/fabpot/sphinx-php/forks',
      keys_url: 'https://api.github.com/repos/fabpot/sphinx-php/keys{/key_id}',
      collaborators_url: 'https://api.github.com/repos/fabpot/sphinx-php/collaborators{/collaborator}',
      teams_url: 'https://api.github.com/repos/fabpot/sphinx-php/teams',
      hooks_url: 'https://api.github.com/repos/fabpot/sphinx-php/hooks',
      issue_events_url: 'https://api.github.com/repos/fabpot/sphinx-php/issues/events{/number}',
      events_url: 'https://api.github.com/repos/fabpot/sphinx-php/events',
      assignees_url: 'https://api.github.com/repos/fabpot/sphinx-php/assignees{/user}',
      branches_url: 'https://api.github.com/repos/fabpot/sphinx-php/branches{/branch}',
      tags_url: 'https://api.github.com/repos/fabpot/sphinx-php/tags',
      blobs_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/blobs{/sha}',
      git_tags_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/tags{/sha}',
      git_refs_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/refs{/sha}',
      trees_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/trees{/sha}',
      statuses_url: 'https://api.github.com/repos/fabpot/sphinx-php/statuses/{sha}',
      languages_url: 'https://api.github.com/repos/fabpot/sphinx-php/languages',
      stargazers_url: 'https://api.github.com/repos/fabpot/sphinx-php/stargazers',
      contributors_url: 'https://api.github.com/repos/fabpot/sphinx-php/contributors',
      subscribers_url: 'https://api.github.com/repos/fabpot/sphinx-php/subscribers',
      subscription_url: 'https://api.github.com/repos/fabpot/sphinx-php/subscription',
      commits_url: 'https://api.github.com/repos/fabpot/sphinx-php/commits{/sha}',
      git_commits_url: 'https://api.github.com/repos/fabpot/sphinx-php/git/commits{/sha}',
      comments_url: 'https://api.github.com/repos/fabpot/sphinx-php/comments{/number}',
      issue_comment_url: 'https://api.github.com/repos/fabpot/sphinx-php/issues/comments{/number}',
      contents_url: 'https://api.github.com/repos/fabpot/sphinx-php/contents/{+path}',
      compare_url: 'https://api.github.com/repos/fabpot/sphinx-php/compare/{base}...{head}',
      merges_url: 'https://api.github.com/repos/fabpot/sphinx-php/merges',
      archive_url: 'https://api.github.com/repos/fabpot/sphinx-php/{archive_format}{/ref}',
      downloads_url: 'https://api.github.com/repos/fabpot/sphinx-php/downloads',
      issues_url: 'https://api.github.com/repos/fabpot/sphinx-php/issues{/number}',
      pulls_url: 'https://api.github.com/repos/fabpot/sphinx-php/pulls{/number}',
      milestones_url: 'https://api.github.com/repos/fabpot/sphinx-php/milestones{/number}',
      notifications_url: 'https://api.github.com/repos/fabpot/sphinx-php/notifications{?since,all,participating}',
      labels_url: 'https://api.github.com/repos/fabpot/sphinx-php/labels{/name}',
      releases_url: 'https://api.github.com/repos/fabpot/sphinx-php/releases{/id}',
      deployments_url: 'https://api.github.com/repos/fabpot/sphinx-php/deployments',
      created_at: '2010-12-16T12:32:24Z',
      updated_at: '2020-02-01T13:52:28Z',
      pushed_at: '2018-04-20T11:28:29Z',
      git_url: 'git://github.com/fabpot/sphinx-php.git',
      ssh_url: '[email protected]:fabpot/sphinx-php.git',
      clone_url: 'https://github.com/fabpot/sphinx-php.git',
      svn_url: 'https://github.com/fabpot/sphinx-php',
      homepage: '',
      size: 38,
      stargazers_count: 154,
      watchers_count: 154,
      language: 'Python',
      has_issues: true,
      has_projects: false,
      has_downloads: true,
      has_wiki: false,
      has_pages: false,
      forks_count: 35,
      mirror_url: null,
      archived: false,
      disabled: false,
      open_issues_count: 6,
      license: [Object],
      forks: 35,
      open_issues: 6,
      watchers: 154,
      default_branch: 'master',
      score: 1
    },
    {
      id: 119659470,
      node_id: 'MDEwOlJlcG9zaXRvcnkxMTk2NTk0NzA=',
      name: 'gitter',
      full_name: 'fabpot/gitter',
      private: false,
      owner: [Object],
      html_url: 'https://github.com/fabpot/gitter',
      description: null,
      fork: false,
      url: 'https://api.github.com/repos/fabpot/gitter',
      forks_url: 'https://api.github.com/repos/fabpot/gitter/forks',
      keys_url: 'https://api.github.com/repos/fabpot/gitter/keys{/key_id}',
      collaborators_url: 'https://api.github.com/repos/fabpot/gitter/collaborators{/collaborator}',
      teams_url: 'https://api.github.com/repos/fabpot/gitter/teams',
      hooks_url: 'https://api.github.com/repos/fabpot/gitter/hooks',
      issue_events_url: 'https://api.github.com/repos/fabpot/gitter/issues/events{/number}',
      events_url: 'https://api.github.com/repos/fabpot/gitter/events',
      assignees_url: 'https://api.github.com/repos/fabpot/gitter/assignees{/user}',
      branches_url: 'https://api.github.com/repos/fabpot/gitter/branches{/branch}',
      tags_url: 'https://api.github.com/repos/fabpot/gitter/tags',
      blobs_url: 'https://api.github.com/repos/fabpot/gitter/git/blobs{/sha}',
      git_tags_url: 'https://api.github.com/repos/fabpot/gitter/git/tags{/sha}',
      git_refs_url: 'https://api.github.com/repos/fabpot/gitter/git/refs{/sha}',
      trees_url: 'https://api.github.com/repos/fabpot/gitter/git/trees{/sha}',
      statuses_url: 'https://api.github.com/repos/fabpot/gitter/statuses/{sha}',
      languages_url: 'https://api.github.com/repos/fabpot/gitter/languages',
      stargazers_url: 'https://api.github.com/repos/fabpot/gitter/stargazers',
      contributors_url: 'https://api.github.com/repos/fabpot/gitter/contributors',
      subscribers_url: 'https://api.github.com/repos/fabpot/gitter/subscribers',
      subscription_url: 'https://api.github.com/repos/fabpot/gitter/subscription',
      commits_url: 'https://api.github.com/repos/fabpot/gitter/commits{/sha}',
      git_commits_url: 'https://api.github.com/repos/fabpot/gitter/git/commits{/sha}',
      comments_url: 'https://api.github.com/repos/fabpot/gitter/comments{/number}',
      issue_comment_url: 'https://api.github.com/repos/fabpot/gitter/issues/comments{/number}',
      contents_url: 'https://api.github.com/repos/fabpot/gitter/contents/{+path}',
      compare_url: 'https://api.github.com/repos/fabpot/gitter/compare/{base}...{head}',
      merges_url: 'https://api.github.com/repos/fabpot/gitter/merges',
      archive_url: 'https://api.github.com/repos/fabpot/gitter/{archive_format}{/ref}',
      downloads_url: 'https://api.github.com/repos/fabpot/gitter/downloads',
      issues_url: 'https://api.github.com/repos/fabpot/gitter/issues{/number}',
      pulls_url: 'https://api.github.com/repos/fabpot/gitter/pulls{/number}',
      milestones_url: 'https://api.github.com/repos/fabpot/gitter/milestones{/number}',
      notifications_url: 'https://api.github.com/repos/fabpot/gitter/notifications{?since,all,participating}',
      labels_url: 'https://api.github.com/repos/fabpot/gitter/labels{/name}',
      releases_url: 'https://api.github.com/repos/fabpot/gitter/releases{/id}',
      deployments_url: 'https://api.github.com/repos/fabpot/gitter/deployments',
      created_at: '2018-01-31T08:43:54Z',
      updated_at: '2020-02-04T16:51:46Z',
      pushed_at: '2018-01-31T08:44:49Z',
      git_url: 'git://github.com/fabpot/gitter.git',
      ssh_url: '[email protected]:fabpot/gitter.git',
      clone_url: 'https://github.com/fabpot/gitter.git',
      svn_url: 'https://github.com/fabpot/gitter',
      homepage: null,
      size: 67,
      stargazers_count: 56,
      watchers_count: 56,
      language: 'PHP',
      has_issues: true,
      has_projects: true,
      has_downloads: true,
      has_wiki: true,
      has_pages: false,
      forks_count: 1,
      mirror_url: null,
      archived: false,
      disabled: false,
      open_issues_count: 0,
      license: [Object],
      forks: 1,
      open_issues: 0,
      watchers: 56,
      default_branch: 'master',
      score: 1
    },
    {
      id: 185822298,
      node_id: 'MDEwOlJlcG9zaXRvcnkxODU4MjIyOTg=',
      name: 'test-releases',
      full_name: 'fabpot/test-releases',
      private: false,
      owner: [Object],
      html_url: 'https://github.com/fabpot/test-releases',
      description: null,
      fork: false,
      url: 'https://api.github.com/repos/fabpot/test-releases',
      forks_url: 'https://api.github.com/repos/fabpot/test-releases/forks',
      keys_url: 'https://api.github.com/repos/fabpot/test-releases/keys{/key_id}',
      collaborators_url: 'https://api.github.com/repos/fabpot/test-releases/collaborators{/collaborator}',
      teams_url: 'https://api.github.com/repos/fabpot/test-releases/teams',
      hooks_url: 'https://api.github.com/repos/fabpot/test-releases/hooks',
      issue_events_url: 'https://api.github.com/repos/fabpot/test-releases/issues/events{/number}',
      events_url: 'https://api.github.com/repos/fabpot/test-releases/events',
      assignees_url: 'https://api.github.com/repos/fabpot/test-releases/assignees{/user}',
      branches_url: 'https://api.github.com/repos/fabpot/test-releases/branches{/branch}',
      tags_url: 'https://api.github.com/repos/fabpot/test-releases/tags',
      blobs_url: 'https://api.github.com/repos/fabpot/test-releases/git/blobs{/sha}',
      git_tags_url: 'https://api.github.com/repos/fabpot/test-releases/git/tags{/sha}',
      git_refs_url: 'https://api.github.com/repos/fabpot/test-releases/git/refs{/sha}',
      trees_url: 'https://api.github.com/repos/fabpot/test-releases/git/trees{/sha}',
      statuses_url: 'https://api.github.com/repos/fabpot/test-releases/statuses/{sha}',
      languages_url: 'https://api.github.com/repos/fabpot/test-releases/languages',
      stargazers_url: 'https://api.github.com/repos/fabpot/test-releases/stargazers',
      contributors_url: 'https://api.github.com/repos/fabpot/test-releases/contributors',
      subscribers_url: 'https://api.github.com/repos/fabpot/test-releases/subscribers',
      subscription_url: 'https://api.github.com/repos/fabpot/test-releases/subscription',
      commits_url: 'https://api.github.com/repos/fabpot/test-releases/commits{/sha}',
      git_commits_url: 'https://api.github.com/repos/fabpot/test-releases/git/commits{/sha}',
      comments_url: 'https://api.github.com/repos/fabpot/test-releases/comments{/number}',
      issue_comment_url: 'https://api.github.com/repos/fabpot/test-releases/issues/comments{/number}',
      contents_url: 'https://api.github.com/repos/fabpot/test-releases/contents/{+path}',
      compare_url: 'https://api.github.com/repos/fabpot/test-releases/compare/{base}...{head}',
      merges_url: 'https://api.github.com/repos/fabpot/test-releases/merges',
      archive_url: 'https://api.github.com/repos/fabpot/test-releases/{archive_format}{/ref}',
      downloads_url: 'https://api.github.com/repos/fabpot/test-releases/downloads',
      issues_url: 'https://api.github.com/repos/fabpot/test-releases/issues{/number}',
      pulls_url: 'https://api.github.com/repos/fabpot/test-releases/pulls{/number}',
      milestones_url: 'https://api.github.com/repos/fabpot/test-releases/milestones{/number}',
      notifications_url: 'https://api.github.com/repos/fabpot/test-releases/notifications{?since,all,participating}',
      labels_url: 'https://api.github.com/repos/fabpot/test-releases/labels{/name}',
      releases_url: 'https://api.github.com/repos/fabpot/test-releases/releases{/id}',
      deployments_url: 'https://api.github.com/repos/fabpot/test-releases/deployments',
      created_at: '2019-05-09T15:08:29Z',
      updated_at: '2019-11-19T09:44:37Z',
      pushed_at: '2019-05-09T15:10:42Z',
      git_url: 'git://github.com/fabpot/test-releases.git',
      ssh_url: '[email protected]:fabpot/test-releases.git',
      clone_url: 'https://github.com/fabpot/test-releases.git',
      svn_url: 'https://github.com/fabpot/test-releases',
      homepage: null,
      size: 3,
      stargazers_count: 2,
      watchers_count: 2,
      language: null,
      has_issues: false,
      has_projects: true,
      has_downloads: true,
      has_wiki: true,
      has_pages: false,
      forks_count: 1,
      mirror_url: null,
      archived: false,
      disabled: false,
      open_issues_count: 0,
      license: null,
      forks: 1,
      open_issues: 0,
      watchers: 2,
      default_branch: 'master',
      score: 1
    },
    {
      id: 237386698,
      node_id: 'MDEwOlJlcG9zaXRvcnkyMzczODY2OTg=',
      name: '.github',
      full_name: 'fabpot/.github',
      private: false,
      owner: [Object],
      html_url: 'https://github.com/fabpot/.github',
      description: null,
      fork: false,
      url: 'https://api.github.com/repos/fabpot/.github',
      forks_url: 'https://api.github.com/repos/fabpot/.github/forks',
      keys_url: 'https://api.github.com/repos/fabpot/.github/keys{/key_id}',
      collaborators_url: 'https://api.github.com/repos/fabpot/.github/collaborators{/collaborator}',
      teams_url: 'https://api.github.com/repos/fabpot/.github/teams',
      hooks_url: 'https://api.github.com/repos/fabpot/.github/hooks',
      issue_events_url: 'https://api.github.com/repos/fabpot/.github/issues/events{/number}',
      events_url: 'https://api.github.com/repos/fabpot/.github/events',
      assignees_url: 'https://api.github.com/repos/fabpot/.github/assignees{/user}',
      branches_url: 'https://api.github.com/repos/fabpot/.github/branches{/branch}',
      tags_url: 'https://api.github.com/repos/fabpot/.github/tags',
      blobs_url: 'https://api.github.com/repos/fabpot/.github/git/blobs{/sha}',
      git_tags_url: 'https://api.github.com/repos/fabpot/.github/git/tags{/sha}',
      git_refs_url: 'https://api.github.com/repos/fabpot/.github/git/refs{/sha}',
      trees_url: 'https://api.github.com/repos/fabpot/.github/git/trees{/sha}',
      statuses_url: 'https://api.github.com/repos/fabpot/.github/statuses/{sha}',
      languages_url: 'https://api.github.com/repos/fabpot/.github/languages',
      stargazers_url: 'https://api.github.com/repos/fabpot/.github/stargazers',
      contributors_url: 'https://api.github.com/repos/fabpot/.github/contributors',
      subscribers_url: 'https://api.github.com/repos/fabpot/.github/subscribers',
      subscription_url: 'https://api.github.com/repos/fabpot/.github/subscription',
      commits_url: 'https://api.github.com/repos/fabpot/.github/commits{/sha}',
      git_commits_url: 'https://api.github.com/repos/fabpot/.github/git/commits{/sha}',
      comments_url: 'https://api.github.com/repos/fabpot/.github/comments{/number}',
      issue_comment_url: 'https://api.github.com/repos/fabpot/.github/issues/comments{/number}',
      contents_url: 'https://api.github.com/repos/fabpot/.github/contents/{+path}',
      compare_url: 'https://api.github.com/repos/fabpot/.github/compare/{base}...{head}',
      merges_url: 'https://api.github.com/repos/fabpot/.github/merges',
      archive_url: 'https://api.github.com/repos/fabpot/.github/{archive_format}{/ref}',
      downloads_url: 'https://api.github.com/repos/fabpot/.github/downloads',
      issues_url: 'https://api.github.com/repos/fabpot/.github/issues{/number}',
      pulls_url: 'https://api.github.com/repos/fabpot/.github/pulls{/number}',
      milestones_url: 'https://api.github.com/repos/fabpot/.github/milestones{/number}',
      notifications_url: 'https://api.github.com/repos/fabpot/.github/notifications{?since,all,participating}',
      labels_url: 'https://api.github.com/repos/fabpot/.github/labels{/name}',
      releases_url: 'https://api.github.com/repos/fabpot/.github/releases{/id}',
      deployments_url: 'https://api.github.com/repos/fabpot/.github/deployments',
      created_at: '2020-01-31T08:10:59Z',
      updated_at: '2020-01-31T08:11:51Z',
      pushed_at: '2020-01-31T08:11:49Z',
      git_url: 'git://github.com/fabpot/.github.git',
      ssh_url: '[email protected]:fabpot/.github.git',
      clone_url: 'https://github.com/fabpot/.github.git',
      svn_url: 'https://github.com/fabpot/.github',
      homepage: null,
      size: 0,
      stargazers_count: 0,
      watchers_count: 0,
      language: null,
      has_issues: true,
      has_projects: true,
      has_downloads: true,
      has_wiki: true,
      has_pages: false,
      forks_count: 0,
      mirror_url: null,
      archived: false,
      disabled: false,
      open_issues_count: 0,
      license: null,
      forks: 0,
      open_issues: 0,
      watchers: 0,
      default_branch: 'master',
      score: 1
    }
  ]};

var lista = result.items;

var listaFinal = lista
       .sort(function(a, b) {
             return a.created_at < b.created_at
        })
       .slice(0,3)
       .map(repo => repo.name);

console.log(listaFinal);

8
  • muito obrigado pela reposta. Mas tem uma questão... Se voce reparar meu código, estou usando um foreach para listar os nomes. No seu exemplo esses nomes ja estão em um array, como posso coloca-los no array usando o foreach? Outro problema é que eu quero ordena-los em ordem crescente mas nao pelo nome e sim pelo item "created_at" da lista de objetos. Pode me ajudar com isso? OBS: estou usando nodejs, preciso instalar algo na pasta nodemodules para usar essa função?
    – SaMuK
    8/02/2020 às 23:17
  • veja a edição que fiz na pergunta incluindo a ordenação por data ;) 8/02/2020 às 23:20
  • Certo, obrigado Ricardo. Então se eu adicionar o código listaFinal.items.forEach(i=> console.log(i.name)); deverá listar apenas os 3 nomes e ja ordenados, certo? Pois o console.log(listaFinal); vai me trazer todo o json, correto?
    – SaMuK
    8/02/2020 às 23:24
  • sim, mas pode fazer isso mais simples ainda usando a função map, para mapear o resultado e retornar só nome. Editei a pergunta com esse exemplo 8/02/2020 às 23:30
  • Ricardo, eu fiz o teste aqui e me parece que nao ordenou corretamente, voce sabe oque pode ser o erro? Coloquei para ao inves de escrever o nome, escreve a data, para verificar se estava correta a ordem, e oque obtive foi isso [ '2015-12-28T11:30:55Z', '2014-03-28T20:24:31Z', '2015-03-14T13:56:32Z', '2017-09-26T20:27:15Z', '2018-02-07T16:19:45Z', voce sabe oque pode ter acontecido?
    – SaMuK
    8/02/2020 às 23:47

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .