如何使用具有特定属性的嵌套对象进行计数?

时间:2015-06-29 02:12:55

标签: javascript json angularjs lodash

好的,所以我使用angular来将json保存到我的计算机上以重新创建github成绩簿。

我可以通过我的$ http请求获取数据但是为了爱我,我想要的只是计算标签问题的数量" Not Yet"。

这是javascript:

$http.get('/api/github/repos/issues/all_issues/00All.json')
  .then(function(response) {
    console.log(response.data[0]);
    var counter = 0;
    for(var index = 0; index < response.data.length; index++) {
      if(response.data[index].labels[0].name == "Not Yet") {
        counter++;
      };
    };
    console.log(counter);
  });

这是最新的尝试,我也尝试使用lodash来提前获得它:

$http.get('/api/github/repos/issues/all_issues/00All.json')
  .then(function(response) {
    console.log(response);
    mile.notYet.width = _.forEach(response.data, function(n){
      var counter = 0;
      if(_.result(_.find(n.labels[0], 'name')) == "Not Yet") {
        counter++;
      }
      console.log(counter);
      counter = ((counter/10) * 100) + '%';
    });
  });

这是一些json数据:

[
  {
    "url": "https://api.github.com/repos/TheIronYard--Orlando/2015--SUMMER--FEE/issues/11",
    "labels_url": "https://api.github.com/repos/TheIronYard--Orlando/2015--SUMMER--FEE/issues/11/labels{/name}",
    "comments_url": "https://api.github.com/repos/TheIronYard--Orlando/2015--SUMMER--FEE/issues/11/comments",
    "events_url": "https://api.github.com/repos/TheIronYard--Orlando/2015--SUMMER--FEE/issues/11/events",
    "html_url": "https://github.com/TheIronYard--Orlando/2015--SUMMER--FEE/issues/11",
    "id": 73013825,
    "number": 11,
    "title": "00 -- Brace Yourself -- BEN GRIFFITH",
    "user": {
      "login": "Epicurean306",
      "id": 11682684,
      "avatar_url": "https://avatars.githubusercontent.com/u/11682684?v=3",
      "gravatar_id": "",
      "url": "https://api.github.com/users/Epicurean306",
      "html_url": "https://github.com/Epicurean306",
      "followers_url": "https://api.github.com/users/Epicurean306/followers",
      "following_url": "https://api.github.com/users/Epicurean306/following{/other_user}",
      "gists_url": "https://api.github.com/users/Epicurean306/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/Epicurean306/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/Epicurean306/subscriptions",
      "organizations_url": "https://api.github.com/users/Epicurean306/orgs",
      "repos_url": "https://api.github.com/users/Epicurean306/repos",
      "events_url": "https://api.github.com/users/Epicurean306/events{/privacy}",
      "received_events_url": "https://api.github.com/users/Epicurean306/received_events",
      "type": "User",
      "site_admin": false
    },
    "labels": [
      {
        "url": "https://api.github.com/repos/TheIronYard--Orlando/2015--SUMMER--FEE/labels/Not%20Yet",
        "name": "Not Yet",
        "color": "e11d21"
      }
    ],

正如你所看到的,labels属性是一个对象,嵌套在一个数组中,嵌套在一个对象中,嵌套在一个数组中,非常可爱。放置标签[0]每次都会给我带来错误,并且不会让我知道。任何人都可以告诉我在哪里搞砸了吗?谢谢!

3 个答案:

答案 0 :(得分:1)

您不需要lodash执行任务

var cnt = response.data
    .map(function(i) { return i.labels; })
         // here we extract labels object only (and get an array of arrays of objects)
    .map(function(i) { return i.filter(function(l) { return l.name == 'Not yet'; }).length; })
         // then for every nested array we return a number of items with 
         // Not Yet names (and get an array of numbers)
    .filter(function(c) { return c > 0; })
         // then we filter issues that don't have one (and still get an array of numbers)
    .length;
         // and finally get length (which is a number)

答案 1 :(得分:1)

如果您需要一个包含lodash的解决方案,它比本机高阶函数更高效,那么您可以尝试以下解决方案:

var size = _(response.data)
   .pluck('labels')
   .flatten()
   .where({ name: 'Not Yet' })
   .size();

更新:

如果希望它更具可重用性,可以保存克隆链序列的引用,并为该克隆序列提供另一个数组。

var data1 = [/*array from data1*/];
var data2 = [/*array from data2*/];

var notYetSequence = _(data1)
  .pluck('labels')
  .flatten()
  .where({ name: 'Not Yet' });

notYetSequence.size(); // returns data 1 count
notYetSequence.plant(data2).size(); // returns data 2 count

答案 2 :(得分:1)

作为比较,plain for循环看起来像:

git pull origin master

所以真的不是很多代码,for循环将与所有浏览器兼容(尽管使用xmlHTTPRequest意味着至少ed 3+)并且最快......当然未经测试。 ; - )