我有这样的json ..
{
"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"
}
}
]
}
]
}
我想在一个数组中获得唯一的release_version ..
我需要另一个数组中的“项目ID”和“名称”键。
如何使用angularjs实现这一目标?这些数据我想在控制器中获取并将这些数组分配给$ scope ..
答案 0 :(得分:2)
我知道这个问题已被接受。而且我喜欢纯粹的javascript。但是,我无法通过 LoDash 提供优雅的解决方案。
据我所知,你需要两个不同的阵列。
在数组中提取 release_version
_.pluck(data['Project_details'][0]['Releases'], 'release_version');
// ["13.2", "14.1", "14.2"]
提取 Project_Id 和名称
_.pick(data['Project_details'][0], ['Project_ID', 'Name'])
// Object {Project_ID: "1245353445", Name: "Test1"}
使用索引0
,因为Project_details数组中只有一行。
答案 1 :(得分:1)
没有什么特别的角度。最终代码(原始javascript):
var x = {
"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"
}
}
]
}
]
};
var y = createMap(selectMany(x.Project_details.map(function (x) { return x.Releases; })).map(function (x) { return x.release_version; }));
console.log(Object.keys(y)); // "13.2" "14.1" "14.2"
function selectMany(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
result.push(arr[i][j]);
}
}
return result;
}
function createMap(arr) {
return arr.reduce(function (result, key) {
result[key] = true;
return result;
}, {});
}
您基本上需要功能selectMany
和createMap
。
我实际上是在TypeScript中编写它,看到类型转换更有趣(但对初学者来说可能更复杂):
let x = {
"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"
}
}
]
}
]
}
let y = createMap(selectMany(x.Project_details.map(x=>x.Releases)).map(x=>x.release_version));
console.log(Object.keys(y));// "13.2" "14.1" "14.2"
function selectMany<T>(arr: T[][]): T[] {
var result = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
result.push(arr[i][j]);
}
}
return result;
}
function createMap(arr: string[]): { [string: string]: boolean } {
return arr.reduce((result: { [string: string]: boolean }, key: string) => {
result[key] = true;
return result;
}, <{ [string: string]: boolean }>{});
}
答案 2 :(得分:0)
使用angular.forEach
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);});