jaylani jaylani - 2 months ago 6
AngularJS Question

How to get particular unique values from json in angularjs

I have my json like this..

{
"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"
}
}
]
}
]
}


I want to get the unique release_version in one array..

And I need the keys "Project ID" and "Name" in another array..

How this can be possible using angularjs? these data I want to get it in controller and assign those arrays to $scope..

Answer

Nothing specific to angular. The final code (raw 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;
    }, {});
}

You basically needed the functions selectMany and createMap.

More

I actually wrote it in TypeScript, its more fun to see the type transforms (but probably more complex for a beginner):

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 }>{});
}