gpt4 book ai didi

javascript - 如何获得具有特定属性的嵌套对象的计数?

转载 作者:行者123 更新时间:2023-11-30 12:20:58 26 4
gpt4 key购买 nike

好的,所以我正在使用 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] 每次都会给我带来错误,并且不会让我计数。谁能告诉我我哪里搞砸了?谢谢!

最佳答案

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

关于javascript - 如何获得具有特定属性的嵌套对象的计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31106814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com