Select items and sort objects list

Asked

Viewed 65 times

-1

I’m learning how to use the Github API and for that I took a user to use as an example. I am getting a list of objects and need to select the names of the 3 oldest items and leave them in ascending order (oldest to the most current).

This is my code(It’s in 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));
}

the command console.log(result); return me the following 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
    }
  ]
}

Already the command result.items.forEach(i=>console.log(i.name)); already returns me only the name of the repositories, which is like this:

sphinx-php
gitter
test-releases
.github

Anyway, the names I already have, but how can I select the oldest 3 and leave them in ascending order(From the oldest to the most current)? Help me please.

1 answer

1


Having the data you want, in your case the repositories list, you can use the sort() to order, and the slice() to take only the first 3 items of the array.
Take this example:

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

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

console.log(listaFinal);

See that the sort is ordering in ascending order. If you need another order, or an object with other attributes, you can pass as parameter a function that sorts, and the slice receives two parameters, first the initial position, second (which is optional) the final position (not included, this is the limit). That is, take the element from zero to 3, not including this, which means take from 0 to 2.

Now as ordered by date, you need a function that sorts by the correct field, for example "created_at". See this other example:

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);

  • thank you so much for the answer. But there is a question... If you repair my code, I am using a foreach to list the names. In your example these names are already in an array, how can I put them in the array using foreach? Another problem is that I want to sort them in ascending order but not by name but by the "created_at" item in the object list. Can you help me with this? NOTE: I am using nodejs, I need to install something in the nodemodules folder to use this function?

  • see the issue I made in the question including the sort by date ;)

  • Okay, thank you Ricardo. So if I add the code listaFinal.items.forEach(i=> console.log(i.name)); should list only the 3 names and already ordered, right? Because the console.log(listaFinal); will bring me all the json, correct?

  • yes, but can make it simpler still using the function map, to map the result and return only name. I edited the question with this example

  • Ricardo, I took the test here and I don’t think you ordered it correctly, Do you know what the mistake might be? I put it down instead of writing the name, write the date, to see if the order was correct, and what I got was this [&#xA; '2015-12-28T11:30:55Z',&#xA; '2014-03-28T20:24:31Z',&#xA; '2015-03-14T13:56:32Z',&#xA; '2017-09-26T20:27:15Z',&#xA; '2018-02-07T16:19:45Z', You know what might have happened?

  • only if you are not correctly comparing the dates when ordering. Try to convert like this: new Date(a.created_at) < new Date(b.created_at)

  • Continued the same way :/

  • this example you have set does not have these dates. Put the json you are using for example in jsfiddle and pass the link to analyze it better

Show 3 more comments

Browser other questions tagged

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