dynamic json array select
by bilbobaggins
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/alasql/0.4.5/alasql.min.js"></script>
<xmp id="output"></xmp>
JavaScript
var workItems = [{
"rolename": "moderator",
"modulename": "employees,inventories",
"accesstype": "AA,VO",
"isactive": true,
"modid": "1,2",
"mroleid": "10,7,8,9",
"roleid": 43
}]
var pply = ["rolename", "modulename", "accesstype"]
let validationmap = [{
"inputtype": "textbox",
"inputname": "rolename",
"inputplaceholder": "Role",
"fieldtypename": "STRING",
"fieldvalidatename": "string",
"fieldmaxlength": "80"
}, {
"inputtype": "multiselect",
"inputname": "modulename",
"inputplaceholder": "Module",
"fieldtypename": "STRING",
"fieldvalidatename": "string",
"fieldmaxlength": "80"
}, {
"inputtype": "multiselect",
"inputname": "accesstype",
"inputplaceholder": "accesstype",
"fieldtypename": "STRING",
"fieldvalidatename": "string",
"fieldmaxlength": "80"
}]
var res = workItems.map(function(data) {
return pply.map(function(da) {
return {
key: da,
vals: [data[da]]
}
})
})[0]
const equijoin = (xs, ys, primary, foreign, sel) => {
const ix = xs.reduce((ix, row) => ix.set(row[primary], row), new Map);
return ys.map(row => sel(ix.get(row[foreign]), row));
};
console.log(JSON.stringify(res))
print (validationmap);
print()
console.log((validationmap))
const result = equijoin(res, validationmap, "key", "inputname",
({
vals
}, {
inputtype,
inputname
}) => ({
inputtype,
inputname,
vals
}));
print(result)
var resss = alasql('SELECT a.*, b.inputtype FROM ? a JOIN ? b ON a.key IN b.inputname ', [res,validationmap]);
print (resss);
function print(x){
if(x!=undefined)
{
document.getElementById('output').textContent += JSON.stringify(x, null, '\t')+"\n";
}
else
{
document.getElementById('output').textContent += "********************* *********************";
}
}