Building Tree Using Javascript and JSON - javascript

i have a JSON array have the following data structure
"Jobs":
[
{ "id": "1", "JobTitle": "Engineer", "PID": "null" },
{ "id": "2", "JobTitle": "Project Manager", "PID": "null" },
{ "id": "5", "JobTitle": "Auditing Manager", "PID": "2" },
{ "id": "7", "JobTitle": "Auditor", "PID": "5" },
{ "id": "6", "JobTitle": "QA Manager", "PID": "5" },
{ "id": "3", "JobTitle": "QA", "PID": "6" },
{ "id": "4", "JobTitle": "Business Analyst", "PID": "2" }
]
i want to write a java script using Jquery and Knockoutjs (optional) to build a team structure (organization) with javascript and html, i have like 1000 record i have tried many recursive loops to handle it with no success.
the out put should be like this
<ul id="root">
<li>Engineer</li> //since their pid is null, then they are root nodes ( yeah not only root)
<li>Project Manager</li>
<ul>
<li>Auditing Manager</li>
<li>Business Analyst</li>
</ul>
and so on, it should handle many levels (depth), somebody will suggest DFS or BFS but i couldn't implement them successfully.

It's midnight, I'm tired, but I can not refuse this challenge. It may not be the fastest solution, but the result is good (http://jsfiddle.net/NbRzB/1/) :
function printNode(jobs, tree, id)
{
var html = '<li>' + jobs[id]['JobTitle'] + '</li>';
if(tree[id] instanceof Array)
{
html += '<li><ul>';
for(var i=0; i<tree[id].length; i++)
{
html += printNode(jobs, tree, tree[id][i]);
}
html += '</ul></li>';
}
return html;
}
var jobs =
[
{ 'id': '1', 'JobTitle': 'Engineer', 'PID': 'null' },
{ 'id': '2', 'JobTitle': 'Project Manager', 'PID': 'null' },
{ 'id': '5', 'JobTitle': 'Auditing Manager', 'PID': '2' },
{ 'id': '7', 'JobTitle': 'Auditor', 'PID': '5' },
{ 'id': '6', 'JobTitle': 'QA Manager', 'PID': '5' },
{ 'id': '3', 'JobTitle': 'QA', 'PID': '6' },
{ 'id': '4', 'JobTitle': 'Business Analyst', 'PID': '2' }
];
// tmp is used to build a better structure id => { attributes }
var tmp = {};
for(var i=0; i<jobs.length; i++)
{
tmp[jobs[i]['id']] =
{
'JobTitle' : jobs[i]['JobTitle'],
'PID' : jobs[i]['PID']
}
}
jobs = tmp;
// end - build better structure
// id => { child_id, child_id, ...}
var tree = {};
// { root_id, root_id, ...}
var root = [];
for(var id in tmp)
{
// no pid ? it is a root
if(jobs[id]['PID'] == 'null')
{
root.push(id);
}
else
{
// Add "id" to "jobs[id]['PID']"'s children
if(tree[jobs[id]['PID']] instanceof Array)
{
tree[jobs[id]['PID']].push(id);
}
else
{
tree[jobs[id]['PID']] = [ id ];
}
}
}
// recursive way
var html = '<ul id="root">';
for(var i=0; i<root.length; i++)
{
html += printNode(jobs, tree, root[i]);
}
html += '</ul>';
// output
$('body').append(html);

Related

Parse array of objects recursively and filter object based on id

i have this array of objects : getCategory (variable)
[
{
"id": "20584",
"name": "Produits de coiffure",
"subCategory": [
{
"id": "20590",
"name": "Coloration cheveux",
"subCategory": [
{
"id": "20591",
"name": "Avec ammoniaque"
},
{
"id": "20595",
"name": "Sans ammoniaque"
},
{
"id": "20596",
"name": "Soin cheveux colorés"
},
{
"id": "20597",
"name": "Protection"
},
{
"id": "20598",
"name": "Nuancier de couleurs"
}
]
},
{
"id": "20593",
"name": "Soins cheveux",
"subCategory": [
{
"id": "20594",
"name": "Shampooing"
},
{
"id": "20599",
"name": "Après-shampooing"
},
{
"id": "20600",
"name": "Masques"
},
and i tried everything i could search in stackoverflow ..
lets say on this array i want to get recursively and object with the specified id .. like 20596 and it should return
{
"id": "20596",
"name": "Soin cheveux colorés"
}
The logic way i am doing is like this :
var getSubcategory = getCategory.filter(function f(obj){
if ('subCategory' in obj) {
return obj.id == '20596' || obj.subCategory.filter(f);
}
else {
return obj.id == '20596';
}
});
dont know what else to do .
Thanks
PS : I dont use it in browser so i cannot use any library . Just serverside with no other library . find dont work so i can only use filter
You need to return the found object.
function find(array, id) {
var result;
array.some(function (object) {
if (object.id === id) {
return result = object;
}
if (object.subCategory) {
return result = find(object.subCategory, id);
}
});
return result;
}
var data = [{ id: "20584", name: "Produits de coiffure", subCategory: [{ id: "20590", name: "Coloration cheveux", subCategory: [{ id: "20591", name: "Avec ammoniaque" }, { id: "20595", name: "Sans ammoniaque" }, { id: "20596", name: "Soin cheveux colorés" }, { id: "20597", name: "Protection" }, { id: "20598", name: "Nuancier de couleurs" }] }, { id: "20593", name: "Soins cheveux", subCategory: [{ id: "20594", name: "Shampooing" }, { id: "20599", name: "Après-shampooing" }, { id: "20600", name: "Masques" }] }] }];
console.log(find(data, '20596'));
console.log(find(data, ''));

Javascript Nested JSON Parsing

I have a json object that object contains main nodes and nested nodes. Each node have a property "type", i want to remove the nodes object which contains the property "type = doc".Please find below example json image format. I attached 2 files one image is the input format and other one is the output format.
var json = {
"navigations": [
{
"disp_order": "1",
"menu_id": "25266",
"nodes": [
{
"disp_order": "2",
"menu_id": "18951",
"type": "DOC"
}
],
"type": "MENU"
},
{
"disp_order": "20",
"menu_id": "25204",
"nodes": [
{
"disp_order": "1",
"menu_id": "10295",
"type": "DOC"
},
{
"disp_order": "10",
"menu_id": "25207",
"nodes": [
{
"disp_order": "999",
"menu_id": "17250",
"type": "DOC"
},
],
"type": "MENU"
},
{
"disp_order": "20",
"menu_id": "25209",
"nodes": [
{
"disp_order": "999",
"menu_id": "18881",
"type": "DOC"
},
],
"type": "MENU"
},
],
"type": "MENU"
},
]
}
https://jsfiddle.net/1hoctvdp/
function deleteNonMenu(obj) {
if (obj.type == "DOC") {
return true;
}
if (obj.nodes) {
for (var i = 0; i < obj.nodes.length; i++) {
var res = deleteNonMenu(obj.nodes[i]);
if (res == true) {
delete obj.nodes[i];
}
}
}
return false;
}
for (var i = 0; i < json.navigations.length; i++) {
var result = deleteNonMenu(json.navigations[i]);
if (result == true) {
delete json.navigations[i];
}
}
console.log(json);
Just as an alternative, you could use this ES6 function, which leaves the original object immutable, and creates the filtered version as return value:
function withoutDocNodes(obj) {
return Object(obj) !== obj ? obj // Primitive value: return w/o change
: Object.assign(Array.isArray(obj) ? [] : {}, // Create array or object
// With following properties:
...Object.keys(obj) // For each property:
// Exclude those with DOC type in their value
.filter( key => Object(obj[key]).type !== 'DOC')
// Get recursive value (also without DOC types)
.map( key => [withoutDocNodes(obj[key]), key] )
// Exclude if resulting value is an empty (object or array)
.filter ( ([o]) => Object.keys(o).length )
// Produce key/value to add: arrays get index as property
.map( ([o, key], i) => ({ [Array.isArray(obj) ? i : key]: o }) )
);
}
const obj = {
"navigations": [
{
"disp_order": "1",
"menu_id": "25266",
"nodes": [
{
"disp_order": "2",
"menu_id": "18951",
"type": "DOC"
}
],
"type": "MENU"
},
{
"disp_order": "20",
"menu_id": "25204",
"nodes": [
{
"disp_order": "1",
"menu_id": "10295",
"type": "DOC"
},
{
"disp_order": "10",
"menu_id": "25207",
"nodes": [
{
"disp_order": "999",
"menu_id": "17250",
"type": "DOC"
},
],
"type": "MENU"
},
{
"disp_order": "20",
"menu_id": "25209",
"nodes": [
{
"disp_order": "999",
"menu_id": "18881",
"type": "DOC"
},
],
"type": "MENU"
},
],
"type": "MENU"
},
]
};
const result = withoutDocNodes(obj);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
This one is also working, try this code :
function mainFunction() {
var data = new Array();
data = excludeDocs(json.navigations);
}
function excludeDocs(nodes) {
var _docs = new Array();
$.each(nodes, function(index, node) {
if(typeof node === 'object') {
if(node.type === 'DOC') {
_docs.push(node.menu_id);
}
else if(typeof node.nodes === 'object') {
var _nodes = excludeDocs(node.nodes);
if(!(typeof nodes === 'object' && nodes.length > 0)) {
delete node.nodes;
}
else {
node.nodes = _nodes;
}
}
}
});
return nodes.filter(function(n) {
return !_docs.includes(n.menu_id);
});
}
Here is a solution using object-scan. It's powerful for data processing once you wrap your head around it.
Note: (1) Expects well behaved input and (2) modifies the input in place
// const objectScan = require('object-scan');
const prune = (type, input) => {
objectScan(['**.nodes[*].type'], {
filterFn: ({ value, gparent, gproperty }) => {
if (value === type) {
gparent.splice(gproperty, 1);
}
}
})(input);
};
const json = { navigations: [{ disp_order: '1', menu_id: '25266', nodes: [{ disp_order: '2', menu_id: '18951', type: 'DOC' }], type: 'MENU' }, { disp_order: '20', menu_id: '25204', nodes: [{ disp_order: '1', menu_id: '10295', type: 'DOC' }, { disp_order: '10', menu_id: '25207', nodes: [{ disp_order: '999', menu_id: '17250', type: 'DOC' }], type: 'MENU' }, { disp_order: '20', menu_id: '25209', nodes: [{ disp_order: '999', menu_id: '18881', type: 'DOC' }], type: 'MENU' }], type: 'MENU' }] };
prune('DOC', json);
console.log(json);
// => { navigations: [ { disp_order: '1', menu_id: '25266', nodes: [], type: 'MENU' }, { disp_order: '20', menu_id: '25204', nodes: [ { disp_order: '10', menu_id: '25207', nodes: [], type: 'MENU' }, { disp_order: '20', menu_id: '25209', nodes: [], type: 'MENU' } ], type: 'MENU' } ] }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#16.0.0"></script>
Disclaimer: I'm the author of object-scan

AngularJS Array Comparison

I have got the following array of Usernames
Usernames = [
{
"id": 1,
"userName": "Jack",
"description": "jack is a nice guy",
"userRoleIds": [
1
]
},
{
"id": 2,
"userName": "Caroline",
"description": "Good girl",
"userRoleIds": [
2,3
]
},
{
"id": 3,
"userName": "Smith",
"description": "Smithyyyy",
"userRoleIds": [
1,2
]
}
]
And an array of userRoles.
userRoles = [
{
id: 1,
roleName: "Admin"
},
{
id: 2,
roleName: "Tester"
},
{
id: 3,
roleName: "Developer"
}
]
What i want to get done is first concat the arrays in in Usernames and userRoles to get the following result.
Usernames = [
{
"id": 1,
"userName": "Jack",
"description": "jack is a nice guy",
"userRoleIds": [
{
"id": 1,
"roleName" : "Admin"
}
]
},
{
"id": 2,
"userName": "Caroline",
"description": "Good girl",
"userRoleIds": [
{
"id": 2,
"roleName" : "Tester"
},
{
"id": 3,
"roleName" : "Developer"
}
]
},...
The second thing i want is to be able to filter for the roleName and userName seperated by pipe signs. As in type something in a text box that searches for userName and roleName for example.
if i type
Caroline, Tester
The result will be
result = [
{
"id": 2,
"userName": "Caroline",
"description": "Good girl",
"userRoleIds": [
2,3
]
},
{
"id": 3,
"userName": "Smith",
"description": "Smithyyyy",
"userRoleIds": [
1,2
]
}
]
What is the best practice for achieving this?
Thanks
Here is how I would do it. I prefer using services and take advantage of their functions to keep code clean.
app.service('UserService', function (PermisionsServices) {
var self = {
'list': [],
'load': function (Users) {//Pass your array of Users
angular.forEach(Users, function (user) {
angular.forEach(user.userRoleIds, function (role) {
self.user.userRolesIds.push(PermisionsServices.get(role));
});
self.list.push(user);
});
}, 'get': function (id) {
for (var i = 0; i < self.list.length; i++) {
var obj = self.list[i];
if (obj.id == id) {
return obj;
}
}
}
};
return self;
});
app.service('PermisionsServices', function () {
var self = {
'list': [],
'load': function (permisions) {//Pass your array of permisions
angular.forEach(permisions, function (permision) {
self.list.push(permision);
});
}, 'get': function (id) {
for (var i = 0; i < self.list.length; i++) {
var obj = self.list[i];
if (obj.id == id) {
return obj;
}
}
}
};
return self;
});
Afterwards, you can use it on your controller:
$scope.users=UserService;
And access each of the users as a separate object which can have multiple object permisions.
NOTE: Building the service (populating it) will of course depend on your app logic and controller, you could just easily remove the "load" function and just hardcode the list object by copy and pasting your arrays.
This is the approach I use to load data from API via resource.
Regards
Edit:
For use on the UI, you would just call:
<div ng-repeat='user in users.list'>
{{user.name}} has {{user.permissions}}
</div>
as the object information is already contained within it.
Edit 2:
If you want to search your data, then you can just add a filter like this:
<div ng-repeat='user in users.list | filter: filterList'>
{{user.name}} has {{user.permissions}}
</div>
And then on the controller:
$scope.filterList = function (user) {
if ($scope.filterTextBox) {
return user.name.indexOf($scope.filterTextBox) == 0;
}
return true;
}
Hope this works for you
I would do with pure JS like this. It won't take more than a single assignment line each.
var Usernames = [
{
"id": 1,
"userName": "Jack",
"description": "jack is a nice guy",
"userRoleIds": [
1
]
},
{
"id": 2,
"userName": "Caroline",
"description": "Good girl",
"userRoleIds": [
2,3
]
},
{
"id": 3,
"userName": "Smith",
"description": "Smithyyyy",
"userRoleIds": [
1,2
]
}
],
userRoles = [
{
id: 1,
roleName: "Admin"
},
{
id: 2,
roleName: "Tester"
},
{
id: 3,
roleName: "Developer"
}
],
modified = Usernames.reduce((p,c) => (c.userRoleIds = c.userRoleIds.map(e => e = userRoles.find(f => f.id == e)),p.concat(c)),[]),
query = ["Caroline","Tester"],
filtered = modified.filter(f => query.includes(f.userName) || f.userRoleIds.some(e => query.includes(e.roleName)));
console.log(JSON.stringify(modified,null,2));
console.log(JSON.stringify(filtered,null,2));
You can use lodash to achieve this.
var role = _.find(userRoles, function(role) {
return role.roleName == 'Tester';
});
_.find(Usernames, function(user) {
return user.userName == 'Caroline' || _.indexOf(user.userRoleIds, role.id)>=0;
});

Manipulating javascript object with underscore

I have a Javascript object with a format like below
"items":
{
"Groups":[
{
"title":"group 1",
"SubGroups":[
{
"title":"sub1",
"id" : "1",
"items":[
{
"title":"Ajax request 1",
},
{
"title":"Ajax request 2",
}
]
},
{
"title":"sub2",
"id" : "2",
"items":[
{
"title":"Ajax request 3",
},
{
"title":"Ajax request 4",
}
]
}
]
}
]
There are n 'Groups', n 'subGroups' and n 'items'.
What I want to do firstly is get all the items from a particular group based on id. This is achieved using:
_.each(items.Groups, function(o) {
result = _.where(o.SubGroups, {
'id': '1'
});
});
which returns
"items":[{"title":"Ajax request 1",},{"title":"Ajax request 2",}]
Then I want to get the rest of the data, excluding the items and parent group I have just retrieved.
I tried this:
_.each(items.Groups, function(o) {
arr = _.without(o.SubGroups, _.findWhere(o.SubGroups, {id: '2'}));
});
But this only returns me the items like this:
{
"title":"sub2",
"id" : "2",
"items":[{"title":"Ajax request 3"},{"title":"Ajax request 4",}]
}
whereas what I need is this:
"items":
{
"Groups":[
{
"title":"group 1",
"SubGroups":[
{
"title":"sub2",
"id" : "2",
"items":[
{
"title":"Ajax request 3",
},
{
"title":"Ajax request 4",
}
]
}
]
}
]
Just try this:
_.each(items.Groups, function(o) {
arr = _.without(o, _.findWhere(o.SubGroups, {id: '2'}));
});
o should be enough => you want to get Groups and not SubGroups.
Following is a pure JS implementation:
JSFiddle.
var data = {
"Groups": [{
"title": "group 1",
"SubGroups": [{
"title": "sub1",
"id": "1",
"items": [{
"title": "Ajax request 1",
}, {
"title": "Ajax request 2",
}]
}, {
"title": "sub2",
"id": "2",
"items": [{
"title": "Ajax request 3",
}, {
"title": "Ajax request 4",
}]
}]
}]
}
var items = [];
var group = [];
data.Groups.forEach(function(o) {
var _tmp = JSON.parse(JSON.stringify(o));
_tmp.SubGroups = [];
o.SubGroups.forEach(function(s) {
if (s.id == "1") {
items.push(s.items);
} else {
_tmp.SubGroups.push(s);
group.push(_tmp)
}
});
});
function printObj(label, obj) {
document.write(label + "<pre>" + JSON.stringify(obj, 0, 4) + "</pre>")
}
printObj("group", group);
printObj("items", items);
Using underscore and using your logic to filter all subgroups:
//array to store subgroup with ID 1
var results = [];
var d = _.each(data.items.Groups, function(o) {
result = _.where(o.SubGroups, {
'id': '1'
});
//add to results array
results.push(result);
});
//make a clone of the earlier object so that you get the parent structure.
var data1 = _.clone(data);
//set the filtered results to the group
data1.items.Groups = results;
//your data as you want
console.log(data1)
Working code here

filter result using 2 JSON

This is my saved localstorage,
[{"industry_Id":1,"merchant_id":2}]
I want to filter below result, to get HP.
{
"industries": [
{
"id": 1,
"name": "oil and gas",
"merchant": [
{
"id": 1,
"name": "ABC",
},
{
"id": 2,
"name": "DEF",
},
{
"id": 3,
"name": "GHJ",
}
]
},
{
"id": 2,
"name": "IT",
"merchant": [
{
"id": 1,
"name": "Apple",
},
{
"id": 2,
"name": "HP",
},
{
"id": 3,
"name": "Google",
}
]
}
]
}
I thought of using multiple $.each but it have to iterate few times and it's quite redundant.
I would prefer using Javascript for loop, that way you can skip iterating over every object once required element is found.
Without jQuery (using for)
var i, j, merchant = null;
for(i = 0; i < data['industries'].length; i++){
if(data['industries'][i]['id'] == arg[0]['industry_Id']){
for(j = 0; j < data['industries'][i]['merchant'].length; j++){
if(data['industries'][i]['merchant'][j]['id'] == arg[0]['merchant_id']){
merchant = data['industries'][i]['merchant'][j];
break;
}
}
if(merchant !== null){ break; }
}
}
With jQuery (using $.each)
var merchant_found = null;
$.each(data['industries'], function(i, industry){
if(industry['id'] == arg[0]['industry_Id']){
$.each(industry['merchant'], function(i, merchant){
if(merchant['id'] == arg[0]['merchant_id']){
merchant_found = merchant;
}
return (!merchant_found);
});
}
return (!merchant_found);
});
var arg = [{"industry_Id":1,"merchant_id":2}];
var data = {
"industries": [
{
"id": 1,
"name": "oil and gas",
"merchant": [
{
"id": 1,
"name": "ABC",
},
{
"id": 2,
"name": "DEF",
},
{
"id": 3,
"name": "GHJ",
}
]
},
{
"id": 2,
"name": "IT",
"merchant": [
{
"id": 1,
"name": "Apple",
},
{
"id": 2,
"name": "HP",
},
{
"id": 3,
"name": "Google",
}
]
}
]
};
var i, j, merchant = null;
for(i = 0; i < data['industries'].length; i++){
if(data['industries'][i]['id'] == arg[0]['industry_Id']){
for(j = 0; j < data['industries'][i]['merchant'].length; j++){
if(data['industries'][i]['merchant'][j]['id'] == arg[0]['merchant_id']){
merchant = data['industries'][i]['merchant'][j];
break;
}
}
if(merchant !== null){ break; }
}
}
console.log(merchant);
document.writeln("<b>Without jQuery:</b><br>");
document.writeln((merchant !== null) ? "Found " + merchant['name'] : "Not found");
var merchant_found = null;
$.each(data['industries'], function(i, industry){
if(industry['id'] == arg[0]['industry_Id']){
$.each(industry['merchant'], function(i, merchant){
if(merchant['id'] == arg[0]['merchant_id']){
merchant_found = merchant;
}
return (!merchant_found);
});
}
return (!merchant_found);
});
console.log(merchant_found);
document.writeln("<br><br><b>With jQuery:</b><br>");
document.writeln((merchant_found) ? "Found " + merchant_found['name'] : "Not found");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
selectors.map(function(selector) {
return data.industries.filter(function(industry) {
return industry.id == selector.industry_Id;
})[0].merchant.filter(function(merchant) {
return merchant.id == selector.merchant_id;
})[0].name;
});
// => DEF
If you want "HP", you want industry 2, not industry 1.
.filter(...)[0] is not really optimal. You could use .find(...), but that is not yet universally supported. Or you could use plain old JavaScript and write for loops instead to make it fast. Or you could use objects with ID keys instead of arrays to make lookups faster.
When it comes into a position where collection of data is what you're processing, I suggest you to take a look at underscore.js. It's not optimal choice for the best performance but it does make you code more readable and makes more sense especially when compared with loop.
Say data is a variable which stores your JSON data.
Try this:
// Given this selector criteria
var select = [{"industry_Id":1,"merchant_id":2}];
function filterByCriteria(criteria, data){
var match = [];
_.each(criteria, function(crit){
function matchIndustry(rec){ return rec.id===crit.industry_Id }
function matchMerchant(rec){ return rec.id===crit.merchant_id }
// Filter by industry id
var industry = _.first(_.where(data.industry, matchIndustry));
// Filter by merchant id
var merchant = _.where(industry.merchant, matchMerchant);
_.each(merchant, function addToMatchResult(m){
match.push(m.name);
});
});
return match;
}
var filteredData = filterByCriteria(select, data);
From snippet above, any merchants which match the search criteria will be taken to the match list. Is it more readable to you?
Do you even need numerical id's? Gets super easy when you don't.
/*
{
"industry": {
"oil and gas":{
"merchant": {
"ABC": {
"name": "ABC oil"
},
"DEF": {
"name": "DEF gas"
},
"GHJ" :{
"name": "GHJ oil and gas"
}
}
},
"IT": {
"merchant": {
"Apple" : {
"name": "Apple computers"
},
"HP": {
"name": "Hewlett Packard"
},
"Google": {
"name": "Google. Maw haw haw"
}
}
}
}
}
*/
var data = '{"industry": {"oil and gas":{"merchant": {"ABC": {"name": "ABC oil"},"DEF": {"name": "DEF gas"},"GHJ" :{"name": "GHJ oil and gas"}}},"IT": {"merchant": {"Apple" : {"name": "Apple computers"},"HP": {"name": "Hewlett Packard"},"Google": {"name": "Google. Maw haw haw"}}}}}';
data = JSON.parse(data);
var merchant = data.industry['IT'].merchant['HP'];
alert(merchant.name);
//console.log(merchant.name);

Categories

Resources