Javascript 如何在angularjs中从json获取特定的唯一值
我的json是这样的Javascript 如何在angularjs中从json获取特定的唯一值,javascript,json,angularjs,Javascript,Json,Angularjs,我的json是这样的 { "Project_details": [ { "Project_ID": "1245353445", "Name": "Test1", "Releases": [ { "sid_ratio": "5.0", "release_version": "13.2", "pre_delivery": {
{
"Project_details": [
{
"Project_ID": "1245353445",
"Name": "Test1",
"Releases": [
{
"sid_ratio": "5.0",
"release_version": "13.2",
"pre_delivery": {
"total": "13",
"blocker": "2",
"critical": "2",
"major": "4",
"normal": "6",
"minor": "1"
},
"post_delivery": {
"total": "3",
"blocker": "2",
"critical": "0",
"major": "1",
"normal": "1",
"minor": "1"
}
},
{
"sid_ratio": "15.0",
"release_version": "14.1",
"pre_delivery": {
"total": "13",
"blocker": "3",
"critical": "2",
"major": "4",
"normal": "6",
"minor": "1"
},
"post_delivery": {
"total": "3",
"blocker": "3",
"critical": "0",
"major": "1",
"normal": "1",
"minor": "1"
}
},
{
"sid_ratio": "15.0",
"release_version": "14.2",
"pre_delivery": {
"total": "13",
"blocker": "0",
"critical": "2",
"major": "4",
"normal": "6",
"minor": "1"
},
"post_delivery": {
"total": "3",
"blocker": "0",
"critical": "0",
"major": "1",
"normal": "1",
"minor": "1"
}
}
]
}
]
}
我想在一个阵列中获得唯一的发布版本
我需要另一个数组中的键“Project ID”和“Name”
如何使用angularjs实现这一点?这些数据我想在控制器中获取,并将这些数组分配给$scope..没有特定于角度的数据。最终代码(原始javascript):
我知道那个问题被接受了。我喜欢纯javascript。但我无法阻止自己使用提供一个优雅的解决方案 正如我正确理解的,您需要两个不同的数组 在数组中提取发布\u版本
_.pluck(data['Project_details'][0]['Releases'], 'release_version');
// ["13.2", "14.1", "14.2"]
提取项目Id和名称
_.pick(data['Project_details'][0], ['Project_ID', 'Name'])
// Object {Project_ID: "1245353445", Name: "Test1"}
使用索引
0
是因为您的项目\u details数组中只有一行。使用angular.forEach从Json获取特定唯一值的简单方法
var notificationcontent = [{"type":"success","title":"title","body":"Your deadline to open an Term deposit and save thousands in taxes is june 20th!","timeout":"0","bodyOutputType":"trustedHtml","clickHandler":""},
{"type":"error","title":"title","body":"This month, Your spent Rs.500.05 on restaurants. This exceeds your budget of Rs.300 by Rs.200.","timeout":"0","bodyOutputType":"trustedHtml","clickHandler":""},
{"type":"warning","title":"title","body":"This month, Your spent Rs.500.05 on Gas and Fuel. This exceeds your budget of Rs.800 by Rs.200.","timeout":"0","bodyOutputType":"trustedHtml","clickHandler":""}];
angular.forEach ( notificationcontent, function(value, key) {alert(value.type);console.log(value.body);});
但这与角度无关。您需要的是在数组上循环并查找其中的内容。你试过什么吗?张贴您遇到问题的代码。谢谢您的回答。我刚试过这个,效果很好。
_.pick(data['Project_details'][0], ['Project_ID', 'Name'])
// Object {Project_ID: "1245353445", Name: "Test1"}
var notificationcontent = [{"type":"success","title":"title","body":"Your deadline to open an Term deposit and save thousands in taxes is june 20th!","timeout":"0","bodyOutputType":"trustedHtml","clickHandler":""},
{"type":"error","title":"title","body":"This month, Your spent Rs.500.05 on restaurants. This exceeds your budget of Rs.300 by Rs.200.","timeout":"0","bodyOutputType":"trustedHtml","clickHandler":""},
{"type":"warning","title":"title","body":"This month, Your spent Rs.500.05 on Gas and Fuel. This exceeds your budget of Rs.800 by Rs.200.","timeout":"0","bodyOutputType":"trustedHtml","clickHandler":""}];
angular.forEach ( notificationcontent, function(value, key) {alert(value.type);console.log(value.body);});