jsonData = [
{
isMaster: false,
selected: false,
ID: 0,
"Profile Type": "Line of Business",
Risk: [
{
isMaster: false,
selected: false,
ID: 0.1,
"Overall Control Effectiveness": "Not Assessed",
"Rating": "0",
Control: [
{
isMaster: false,
selected: false,
ID: 0.2,
"Control Classification": "Key",
"Control Effectiveness Rating": "Partially Effective"
},
{
isMaster: false,
selected: false,
ID: 0.21,
"Control Classification": "Compensating",
"Control Effectiveness Rating": "Partially Effective"
}
]
},
{
isMaster: false,
selected: false,
ID: 0.11,
"Overall Control Effectiveness":"Insignificant",
"Rating": "0",
Control: [
{
isMaster: false,
selected: false,
ID: 0.12,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Partially Effective"
},
{
isMaster: false,
selected: false,
ID: 0.13,
"Control Classification": "Compensating",
"Control Effectiveness Rating": "Effective"
}
]
}
]
},
{
isMaster: false,
selected: false,
ID: 1,
"Profile Type": "Business Unit (BU) / Support Unit (SU)",
Risk: [
{
isMaster: false,
selected: false,
ID: 1.1,
"Overall Control Effectiveness": "Low",
"Rating": "2",
Control: [
{
isMaster: false,
selected: false,
ID: 1.2,
"Control Classification": "key",
"Control Effectiveness Rating": "Partially Effective"
},
{
isMaster: false,
selected: false,
ID: 1.21,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Partially Effective"
},
{
isMaster: false,
selected: false,
ID: 1.22,
"Control Classification": "key",
"Control Effectiveness Rating": "Effective"
},
]
},
{
isMaster: false,
selected: false,
ID: 1.11,
"Overall Control Effectiveness": "Medium",
"Rating": "5",
}
]
},
{
isMaster: false,
selected: false,
ID: 2,
Name: "0940375C025200FAA38ED98A F9DE03D61ADAB727BA8C26D4",
"Business Profile Owner": "Susheel",
Folder: "CBA / Audit",
"Profile Type": "Business Profile Instances",
Risk: [
{
isMaster: false,
selected: false,
ID: 2.1,
"Overall Control Effectiveness": "High",
"Rating": "5",
Control: [
{
isMaster: false,
selected: false,
ID: 2.2,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Not Determined"
},
{
isMaster: false,
selected: false,
ID: 2.21,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Not Tested"
}
]
},
{
isMaster: false,
selected: false,
ID: 2.11,
"Overall Control Effectiveness": "very High",
"Rating": "0",
}
]
},
{
isMaster: false,
selected: false,
ID: 3,
"Profile Type": "Supplier",
Risk: [
{
isMaster: false,
selected: false,
ID: 3.1,
"Overall Control Effectiveness":"Lindgren",
"Rating": "0",
Control: [
{
isMaster: false,
selected: false,
ID: 3.2,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Not Tested"
},
{
isMaster: false,
selected: false,
ID: 3.21,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Not Tested"
}
]
},
{
isMaster: false,
selected: false,
ID: 3.11,
"Overall Control Effectiveness": "Lindgren",
"Rating": "3",
}
]
},
];
Multi-select values are of the following types.
selection = [
{
name: "High",
type: "Risk",
column: "Overall Control Effectiveness"
},
{
name: "5",
type: "Risk",
column: "Rating"
}
]
selection1 = [
{
name: "key",
type: "Control",
column: "Control Classification"
},
{
name: "Not Assessed",
type: "Risk",
column: "Overall Control Effectiveness"
}
]
selection2 = [
{
name: "key",
type: "Control",
column: "Control Classification"
},
{
name: "Not Assessed",
type: "Risk",
column: "Overall Control Effectiveness"
},{
name: "0",
type: "Risk",
column: "Rating"
}
]
these selections are of type Dynamic.
if the selections of having only the same types (eg: type=”Risk”) we need to print all matching values.
nothing but || (OR operation)
if the selections of having the different types (eg: type=”Risk” , type=”Control”) we need to print satisfying values as nothing but && (AND operation).
if selection2 is selected then the following will be the output.
{
isMaster: false,
selected: false,
ID: 0,
"Profile Type": "Line of Business",
Risk: [
{
isMaster: false,
selected: false,
ID: 0.1,
"Overall Control Effectiveness": "Not Assessed",
"Rating": "0",
Control: [
{
isMaster: false,
selected: false,
ID: 0.2,
"Control Classification": "Key",
"Control Effectiveness Rating": "Partially Effective"
}
]
}
]
}
const jsonData = [
{
isMaster: false,
selected: false,
ID: 0,
"Profile Type": "Line of Business",
Risk: [
{
isMaster: false,
selected: false,
ID: 0.1,
"Overall Control Effectiveness": "Not Assessed",
"Rating": "0",
Control: [
{
isMaster: false,
selected: false,
ID: 0.2,
"Control Classification": "Key",
"Control Effectiveness Rating": "Partially Effective"
},
{
isMaster: false,
selected: false,
ID: 0.21,
"Control Classification": "Compensating",
"Control Effectiveness Rating": "Partially Effective"
}
]
},
{
isMaster: false,
selected: false,
ID: 0.11,
"Overall Control Effectiveness":"Insignificant",
"Rating": "0",
Control: [
{
isMaster: false,
selected: false,
ID: 0.12,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Partially Effective"
},
{
isMaster: false,
selected: false,
ID: 0.13,
"Control Classification": "Compensating",
"Control Effectiveness Rating": "Effective"
}
]
}
]
},
{
isMaster: false,
selected: false,
ID: 1,
"Profile Type": "Business Unit (BU) / Support Unit (SU)",
Risk: [
{
isMaster: false,
selected: false,
ID: 1.1,
"Overall Control Effectiveness": "Low",
"Rating": "2",
Control: [
{
isMaster: false,
selected: false,
ID: 1.2,
"Control Classification": "key",
"Control Effectiveness Rating": "Partially Effective"
},
{
isMaster: false,
selected: false,
ID: 1.21,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Partially Effective"
},
{
isMaster: false,
selected: false,
ID: 1.22,
"Control Classification": "key",
"Control Effectiveness Rating": "Effective"
},
]
},
{
isMaster: false,
selected: false,
ID: 1.11,
"Overall Control Effectiveness": "Medium",
"Rating": "5",
}
]
},
{
isMaster: false,
selected: false,
ID: 2,
Name: "0940375C025200FAA38ED98A F9DE03D61ADAB727BA8C26D4",
"Business Profile Owner": "Susheel",
Folder: "CBA / Audit",
"Profile Type": "Business Profile Instances",
Risk: [
{
isMaster: false,
selected: false,
ID: 2.1,
"Overall Control Effectiveness": "High",
"Rating": "5",
Control: [
{
isMaster: false,
selected: false,
ID: 2.2,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Not Determined"
},
{
isMaster: false,
selected: false,
ID: 2.21,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Not Tested"
}
]
},
{
isMaster: false,
selected: false,
ID: 2.11,
"Overall Control Effectiveness": "very High",
"Rating": "0",
}
]
},
{
isMaster: false,
selected: false,
ID: 3,
"Profile Type": "Supplier",
Risk: [
{
isMaster: false,
selected: false,
ID: 3.1,
"Overall Control Effectiveness":"Lindgren",
"Rating": "0",
Control: [
{
isMaster: false,
selected: false,
ID: 3.2,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Not Tested"
},
{
isMaster: false,
selected: false,
ID: 3.21,
"Control Classification": "Arrangement",
"Control Effectiveness Rating": "Not Tested"
}
]
},
{
isMaster: false,
selected: false,
ID: 3.11,
"Overall Control Effectiveness": "Lindgren",
"Rating": "3",
}
]
},
];
const selection2 = [
{
name: "key",
type: "Control",
column: "Control Classification"
},
{
name: "Not Assessed",
type: "Risk",
column: "Overall Control Effectiveness"
},{
name: "0",
type: "Risk",
column: "Rating"
}
];
function isArray(val) {
return Array.isArray(val);
}
function isObject(val) {
return val && typeof val === 'object' && val.constructor === Object;
}
function onFilter(selection2) {
let finalOutput = [];
let selectionType = [];
selection2.map(function items(i) {
selectionType.push(i.type)
})
let templist = jsonData;
templist = templist.filter((item) => {
if (isObject(item)) {
objectLoop(item);
function objectLoop(item) {
Object.keys(item).forEach((key) => {
selection2.map(function items(i) {
if(items[key] == i.column) {
}
})
if (isObject(item[key])) {
objectLoop(item[key]);
}
if (isArray(item[key])) {
item[key].map((subItems) => {
if (isObject(subItems)) {
objectLoop(subItems);
}
});
}
});
}
}
return item;
});
console.log(templist)
}
onFilter(selection2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>