0

Summary :

There are three dropdowns for each levelNum. such as dropdown for levelNum2 contains (Department-Unit-1, Department-Unit-2 & Department-Unit-3),levelNum3 contains (Division-Unit-1 & Division-Unit-2) and levelNum4 contains (Business-Unit-1 & Business-Unit-2).

There is an array of objects.Inside each object there is a property named hierarchyLevels which is again an array of objects.Inside each object there is a two property unitName & levelNum as shown in below JSON.

var data = [{
    "hierarchyLevels": [{
        "unitName": "Department-Unit-3",
        "levelNum": 2
    }, {
        "unitName": "Division-Unit-2",
        "levelNum": 3
    }, {
        "unitName": "Business-Unit-1",
        "levelNum": 4
    }]
}, {
    "hierarchyLevels": [{
        "unitName": "Department-Unit-1",
        "levelNum": 2
    }, {
        "unitName": "Division-Unit-1",
        "levelNum": 3
    }, {
        "unitName": "Business-Unit-2",
        "levelNum": 4
    }]
}, {
    "hierarchyLevels": [{
        "unitName": "Department-Unit-2",
        "levelNum": 2
    }, {
        "unitName": "Business-Unit-1",
        "levelNum": 4
    }]
}]

Tried so far :

function getMultipleObjectFromList(propertyName, value, list){
    return list.filter(function (item) {
        return item.hierarchyLevels.some(function (level) {
            return level[propertyName] === value;
        });
    });
};

var res = getMultipleObjectFromList('unitName','Business-Unit-1',data);

Requirement :

I want to fetch all unitName associated with another unitName. So, if I select Business-Unit-1 from the levelNum4 dropdown, the other low level dropdown will auto fill with unitName associated with Business-Unit-1.i.e levelNum3 dropdown will contain only Division-Unit-2 and levelNum2 dropdown will contain (Department-Unit-2 & Department-Unit-3).

8
  • Please share what you have tried so far. Commented May 12, 2016 at 16:56
  • @BlazeSahlzen, i updated the question with tried so far section. Commented May 12, 2016 at 17:32
  • 1
    Does substituting if(tempObj[propertyName] == value) with if(tempObj.hierarchyLevels[propertyName] === value) solves your problem? In case it does not help, can you post how the result of your function should look like? What structure should the resulting object have? Commented May 12, 2016 at 17:47
  • 2 questions: 1. When you say "the other low level dropdown" you mean dropdowns with levelNum less than the selected one? 2. How do you determine the association between two or more unitName? Commented May 12, 2016 at 17:50
  • Any reason why levelNum starts at 2? Where is 1? Does your data structure have to be like this, or can we suggest a better one? Commented May 12, 2016 at 18:39

1 Answer 1

2

Your data structure has more levels than you treat in your function. There is the hierarchyLevels property which you omit, and it is an array, which you do not iterate.

Here is a proposed adapted function that returns those hierarchyLevels that match. I also add a similar function that returns the inner object when there is a match, instead of the hierarchyLevels array. That one can be used to populate your drop-downs:

function getMultipleObjectFromList(propertyName, value, list){
    return list.filter(function (item) {
        return item.hierarchyLevels.some(function (level) {
            return level[propertyName] === value;
        });
    });
};

function getDetailFromList(propertyName, value, list){
    return list.map(function (item) {
        return item.hierarchyLevels.filter(function (level) {
            return level[propertyName] === value;
        }).pop(); // only return single match
    }).filter(function (item) { // exclude null
        return item;
    }); 
};


var data = [{
    "hierarchyLevels": [{
        "unitName": "Department-Unit-3",
        "levelNum": 2
    }, {
        "unitName": "Division-Unit-2",
        "levelNum": 3
    }, {
        "unitName": "Business-Unit-1",
        "levelNum": 4
    }]
}, {
    "hierarchyLevels": [{
        "unitName": "Department-Unit-1",
        "levelNum": 2
    }, {
        "unitName": "Division-Unit-1",
        "levelNum": 3
    }, {
        "unitName": "Business-Unit-2",
        "levelNum": 4
    }]
}, {
    "hierarchyLevels": [{
        "unitName": "Department-Unit-2",
        "levelNum": 2
    }, {
        "unitName": "Business-Unit-1",
        "levelNum": 4
    }]
}];

var res = getMultipleObjectFromList('unitName','Business-Unit-1',data);

var level2 = getDetailFromList('levelNum', 2, res);

var level3 = getDetailFromList('levelNum', 3, res);

var output = '<h2>filtered data:</h2>' + JSON.stringify(res, null, 4) + '\n'
           + '<h2>level 2:</h2>' + JSON.stringify(level2, null, 4)  + '\n'
           + '<h2>level 3:</h2>' + JSON.stringify(level3, null, 4);

document.querySelector('pre').innerHTML = output;
<pre></pre>

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

2 Comments

need your small help again..now i am facing problem with combination of two. now i want levelNum2 object mapped with both (levelNum3 and levelNum4).Example : If i select Business-Unit-2 from 1st dropdown and Division-Unit-1 from second dropdown. Then third dropdown should only contain Department-Unit-1 which is mapped with both.
just call getMultipleObjectFromList twice. First on data, and the second time on the result of the first call. Something like res = getMultipleObjectFromList('unitName', 'Business-Unit-2', getMultipleObjectFromList('unitName', 'Division-Unit-1', data)); ... etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.