Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取具有特定属性的嵌套对象的计数?_Javascript_Json_Angularjs_Lodash - Fatal编程技术网

Javascript 如何获取具有特定属性的嵌套对象的计数?

Javascript 如何获取具有特定属性的嵌套对象的计数?,javascript,json,angularjs,lodash,Javascript,Json,Angularjs,Lodash,好的,我正在使用angular将json保存到我的计算机中,以重新创建github成绩册 我可以通过我的$http请求获取数据,但出于对我的爱,我只想通过标签“尚未”获得问题数量的计数 下面是javascript: $http.get('/api/github/repos/issues/all_issues/00All.json') .then(function(response) { console.log(response.data[0]); var counter =

好的,我正在使用angular将json保存到我的计算机中,以重新创建github成绩册

我可以通过我的$http请求获取数据,但出于对我的爱,我只想通过标签“尚未”获得问题数量的计数

下面是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);
  });
这是一点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]都会导致我出错,并且无法获取计数。谁能告诉我我把事情搞砸了吗?谢谢大家!

这项任务不需要洛达斯

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)

如果您需要一个包含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

作为比较,普通for循环如下所示:

  var data = response.data;
  var count = 0;
  var re = /not yet/i;

  for (var a, i=0, iLen=data.length; i<iLen; i++) {
    a = data[i].labels;
    for (var j=0, jLen=a.length; j<jLen; j++) {
      if (re.test(a[j].name)) ++count;
    }
  }
var data=response.data;
var计数=0;
var re=/尚未/i;

对于(var a,i=0,iLen=data.length;ii如果我在你的家庭作业@gatopazz中找到这段代码,我最好找到一个解释。;)另外,最好使用Lodash实现跨浏览器兼容性:并非所有浏览器实现都在
数组
上提供迭代方法。例如,
filter
仅适用于IE9+:@ALtheX–map和filter功能很容易被For循环替换(执行速度也会更快)。我认为性能方面的最大问题是构建了这么多临时阵列(两个map和两个filter)所有函数调用:除了map和filter之外,还有4个回调。与普通for循环相比,它唯一的函数调用是使用正则表达式或indexOf对每个label对象测试一次“Not-yet”。@RobG我的思想转移到流处理和反应+函数编程,我不能再编写命令式循环了;'-(附言:+1'd you当然:)。上面的代码惰性地计算代码。事实上,它只执行一个循环。一个参考是。因此,如果它是1个循环语句,而不是3个语句,那么它更像是一个“性能稍高一点”而不是“性能稍高一点”。不管怎样,您如何量化“性能稍高一点”和“性能更高一点”?这只是一个文字游戏。它是:-)这里没有过滤,而延迟评估也没有帮助,因为您仍然需要获得整个集合的大小。因此,lodash可能带来的唯一好处是为临时阵列分配内存,对于每秒不到几万次的操作和不到10秒的数千个元素的阵列来说,这是可以忽略的。我同意lodash可以提高性能,但在非常特定的用例中。当然不是一般的。你可能是对的,也许这是有争议的。就好处而言,你可以说lodash有很多因素可以发挥作用:浏览器兼容性、简洁性、可读性和“一点点性能提升”。
  var data = response.data;
  var count = 0;
  var re = /not yet/i;

  for (var a, i=0, iLen=data.length; i<iLen; i++) {
    a = data[i].labels;
    for (var j=0, jLen=a.length; j<jLen; j++) {
      if (re.test(a[j].name)) ++count;
    }
  }