使用reduce从一个数组获取数据到另一个数组

时间:2018-04-24 09:13:43

标签: jquery

我有这个脚本按日期对数据(提交消息)进行分组。

我想添加一个链接到每个消息与个人sha。

如何将第一个data对象中的数据包含到newData对象中,以便最终得到如下变量:var commitUrl = gitHubUrl + sha;可以添加到每个href对象中function fetchDatafromGitHub() { var user = "google"; var repo = "coding-with-chrome" var gitHubUrl = "https://github.com/" + user + "/" + repo + "/commit/"; var url = "https://api.github.com/repos/" + user + "/" + repo + "/commits"; $.ajax({ type: 'GET', url: url, dataType: "json", success: function(data) { console.log(data); var newData = data.reduce((acc, el) => { var date = el.commit.committer.date; var msg = el.commit.message; var sha = el.sha; console.log(sha); var d = date.split('T')[0]; if (acc.hasOwnProperty(d)) acc[d].push(msg); else acc[d] = [msg]; return acc; }, {}) Object.keys(newData).forEach(function(v, k) { //console.log(newData); var date = v; var message = newData[v].join(", "); var t = new Date(date); var day = ("0" + ( t.getDate())).slice(-2); var month = ("0" + ( t.getMonth() + 1)).slice(-2); var year = t.getFullYear(); //var commitUrl = gitHubUrl + sha; gitHubStats.append('<div>' + day + '/' + month + '/' + year + ': <a href="#" target="_blank">' + message + '</a></div>'); }); }, error: function(xhr, status, error) { console.log(xhr.responseText); } }); }

剧本:

  .btn {
     border: none;
     color: grey;
     padding: 12px 16px;
     font-size: 16px;
     cursor: pointer;
     background-image: url("http://alexfeds.com/wp- 
     content/uploads/2018/04/save_icon.svg");
     background-repeat: repeat-y;
       }

   <button class="btn"> Save</button>

JsFiddle here

非常感谢。

1 个答案:

答案 0 :(得分:0)

有很多可能的方法,请查看其中一个解决方案。

这里我通过newData对象中的commitUrl设置href传递消息。并附加在最终消息中。希望它有所帮助

{ "name" : "A", "action" : "walk" }
{ "name" : "D", "play" : "walk" }
{ "name" : "E", "move" : "walk" }
var gitHubStats = $('body');

function fetchDatafromGitHub() {
  var user = "google";
  var repo = "coding-with-chrome"
  var gitHubUrl = "https://github.com/" + user + "/" + repo + "/commit/";
  var url = "https://api.github.com/repos/" + user + "/" + repo + "/commits";
  var maxHits = 5;
  $.ajax({
    type: 'GET',
    url: url,
    dataType: "json",
    success: function(data) {
      //console.log(data);
      var newData = data.reduce((acc, el) => {
        var date = el.commit.committer.date;
        var msg = el.commit.message;
        var sha = el.sha;
        var d = date.split('T')[0];
        
        var commitUrl = gitHubUrl + sha;
        
        var temp_message = '<a href="' + commitUrl + '" target="_blank">' + msg + '</a>';
        
        if (acc.hasOwnProperty(d))
          acc[d].push(temp_message);
        else
          acc[d] = [temp_message];
        return acc;
      }, {})
      
      Object.keys(newData).forEach(function(v, k) {
        var date = v;
        //var message = newData[v].join(", ");
        var t = new Date(date);
        var day = ("0" + (
          t.getDate())).slice(-2);
        var month = ("0" + (
          t.getMonth() + 1)).slice(-2);
        var year = t.getFullYear();
        //var commitUrl = gitHubUrl + sha;
        
        gitHubStats.append('<div>' + day + '/' + month + '/' + year + ': ' + newData[v] + '</div>');
      });
    },
    error: function(xhr, status, error) {
      console.log(xhr.responseText);
    }
  });
}
fetchDatafromGitHub();
div+div {
  margin-top: 10px;
  border-top: 1px solid #333;
  padding: 10px;
}