1

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..

1
  • This isn't really Angular related though. What you want is to loop over an array and looks for stuff in it. Have you tried anything? Post the code that you're having trouble with. Commented Oct 13, 2015 at 5:14

3 Answers 3

2

I know that question was accepted. And I love pure javascript. But I can't keep myself from providing an elegant solution with the use of LoDash.

As I understand correctly, you want two different array.

Extract release_version in an array

_.pluck(data['Project_details'][0]['Releases'], 'release_version');
// ["13.2", "14.1", "14.2"]

Extract Project_Id and Name

_.pick(data['Project_details'][0], ['Project_ID', 'Name'])
// Object {Project_ID: "1245353445", Name: "Test1"}

The index 0 is used cause there only one row in your Project_details array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I just tried this, its working fine.
1

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

Comments

0

Easy way to get the particular unique value from the Json using 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);});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.