Extract object and create an array dynamically based on object keys (check if category.role has value developer)

Report
Question

Please briefly explain why you feel this question should be reported .

Report
Cancel

Hi here is an sample JSON:

$scope.data = [{ “name”: “john”, “age”: “27”, “category”: { “id”: “001”, “role”: “developer” } }, { “name”: “David”, “age”: “27”, “category”: { “id”: “002”, “role”: “Manager” } }, { “name”: “Dave”, “age”: “27”, “category”: { “id”: “001”, “role”: “developer” } }];

Output: { “developer”: [{ “name”: “john”, “age”: “27”, “category”: { “id”: “001”, “role”: “developer” } }, { “name”: “Dave”, “age”: “27”, “category”: { “id”: “001”, “role”: “developer” } }], “Manager”: [{ “name”: “David”, “age”: “27”, “category”: { “id”: “002”, “role”: “Manager” } }] }

var result = [];

$.each(data, function( key, value ) {
    if(value.category.role === "developer")
         result.push(value);
});
How to compare the role when its dynamic? and form an output mentioned above.
in progress 0
JavaScript 8 months 1 Answer 1259 views 0

About DPK2017

Answer ( 1 )

  1. w3r

    Please briefly explain why you feel this answer should be reported .

    Report
    Cancel

    If your JSON dynamic data looks like –
    var x={‘key’:’value’};
    You may fetch key and value this way:
    for(y in x)
    {
    console.log(y);// key
    var val =x[y];// value
    }

Leave an answer

Browse

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>