Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JSON Array similar to the structure below.

[{
"variable_name": "VAR1_XYZ",
"variable_values": [
            {
                "DomainName": "Env1",
                "Value": "ABC Process 1",
                 (some more key-value pairs)
            },
            {
                "DomainName": "Env2",
                "Value": "DEF Process 1",
                 (some more key-value pairs)
            }],
 },{
"variable_name": "VAR2_UVW",
"variable_values": [
            {
                "DomainName": "Env1",
                "Value": "ABC Process 2",
                 (some more key-value pairs)
            },
            {
                "DomainName": "Env2",
                "Value": "DEF Process 2",
                 (some more key-value pairs)
            }],
 }];

I am presenting this data in a table structure with the "variable_name" (as rows / cells) value's across multiple "DomainName"s (as columns) in an "ng-grid".

I am also having a requirement where the user up on selecting one of the "DomainName"s, the values of the "variable_name"s in the selected Domain would have to be compared against the values with the other Domains and depending on whether there is a match or not, the "ng-grid" table cells need to be color coded as "green" or "red" depending on whether there is a match or not..

I had been looking through plain javascript / json object manipulation and underscore / lodash to understand which is the most efficient way to compare JSON objects with the following structures and I could not come to any conclusion on how to achieve it. I am also a newbie to the lodash / underscore library and may be that is causing this confusion if I am over engineering or if I am taking the right path to solve this problem.

My end goal is to create a generic function that takes the selected domain name and the below array to provide an output array that has a list of true / false boolean values for each "variable_name" / "DomainName" that I can use for coloring the grid.

So, for example, if the user selects DomainName as "Env1", I am looking to get all "variable_name"'s under "Env1" and compare the values of the respective "variable_name"s from other domains and create a result object similar to

{Env1 : true, Env2: false, Env3 : true}

that I can use for coloring the ng-grid. Could you please suggest on what is the approach that should be taken if this has to be done using lodash / underscore? instead of plain javascript?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Depending which browsers you're supporting, you could do something like this with vanilla ES5:

var arr = [{
    "DomainName": "Env1",
    "Value": "ABC Process 1"
}, {
    "DomainName": "Env2",
    "Value": "DEF Process 1"
},
{
    "DomainName": "Env3",
    "Value": "DEF Process 1"
}];

function constructDomainObject(collection, selectedDomains) {
    return collection.reduce(function (memo, domain) {
        var domainName = domain["DomainName"];
        var selected = selectedDomains.some(function (domain) { 
            return domain === domainName 
        });
        memo[domainName] = selected;
        return memo;
    }, {});
}

var data = constructDomainObject(arr, ['Env1', 'Env3']); 
// data = {Env1 : true, Env2: false, Env3 : true}

If you're supporting legacy browsers, you could polyfill reduce and some or use the analogous lodash/underscore methods.

Fiddle

share|improve this answer
    
Thank you for your solution and the fiddle. Though the solution you suggested itself is comparing the domain names, I wanted to compare the values for the "variable_name" for each domain. I should still be able to use the solution suggested with minimal changes. Thanks for the help. Appreciate it. –  Satya Feb 17 at 22:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.