how to parse json array and get expected result? - javascript

I am parsing the json .I am checking json array and after that make another object .I just struck in one place.Actually I am checking that if parent have child I add one object in "testCaseList "array that against child .But I need to check if child id have character "not" it should add in this array "commandList"
http://jsfiddle.net/tJ7Kq/5/
Here is my input .
[
{
"id": "a",
"text": "a",
"icon": true,
"li_attr": {
"id": "a"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
},
{
"id": "b",
"text": "b\n ",
"icon": true,
"li_attr": {
"id": "b"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
{
"id": "b-a-1",
"text": "b-a",
"icon": true,
"li_attr": {
"id": "b-a-1"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
},
{
"id": "b-b-2",
"text": "b-b\n ",
"icon": true,
"li_attr": {
"id": "b-b-2"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
{
"id": "b-b-a",
"text": "b-b-a",
"icon": true,
"li_attr": {
"id": "b-b-a"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
},
{
"id": "b-b-b",
"text": "b-b-b",
"icon": true,
"li_attr": {
"id": "b-b-b"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
}
]
}
]
},
{
"id": "c-1",
"text": "c\n ",
"icon": true,
"li_attr": {
"id": "c-1"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
{
"id": "not-c-a-1",
"text": "c-a",
"icon": true,
"li_attr": {
"id": "not-c-a-1"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
},
{
"id": "not-c-b-2",
"text": "b-b",
"icon": true,
"li_attr": {
"id": "not-c-b-2"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
}
]
}
]
getting out put this
[
{
"a": {
"commandList": [],
"testCaseList": []
}
},
{
"b": {
"commandList": [],
"testCaseList": [
{
"b-a-1": {
"commandList": [],
"testCaseList": []
}
},
{
"b-b-2": {
"commandList": [],
"testCaseList": [
{
"b-b-a": {
"commandList": [],
"testCaseList": []
}
},
{
"b-b-b": {
"commandList": [],
"testCaseList": []
}
}
]
}
}
]
}
},
{
"c-1": {
"commandList": [],
"testCaseList": [
{
"not-c-a-1": {
"commandList": [],
"testCaseList": []
}
},
{
"not-c-b-2": {
"commandList": [],
"testCaseList": []
}
}
]
}
}
]
Expected out put is :
[
{
"a": {
"commandList": [],
"testCaseList": []
}
},
{
"b": {
"commandList": [],
"testCaseList": [
{
"b-a-1": {
"commandList": [],
"testCaseList": []
}
},
{
"b-b-2": {
"commandList": [],
"testCaseList": [
{
"b-b-a": {
"commandList": [],
"testCaseList": []
}
},
{
"b-b-b": {
"commandList": [],
"testCaseList": []
}
}
]
}
}
]
}
},
{
"c-1": {
"commandList": [
{
"not-c-a-1": {
"commandList": [],
"testCaseList": []
}
},
{
"not-c-b-2": {
"commandList": [],
"testCaseList": []
}
}],
"testCaseList": []
}
}
]

This mapItem function will do what you need:
function mapItem(inputItem) {
var item = {};
item[inputItem.id] = JSON.parse(sessionStorage.getItem(inputItem.id));
for (k in inputItem.children) {
if (/^not-/.test(inputItem.children[k].id)) {
item[inputItem.id].commandList.push(mapItem(inputItem.children[k]));
}else{
item[inputItem.id].testCaseList.push(mapItem(inputItem.children[k]));
}
}
return item;
}

Related

How to filter nested array in typescript?

I have a JSON as below
{
"Header": {
"Time": "2020-06-09T07:03:20-07:00",
"ReportName": "JournalReport",
"StartPeriod": "2020-06-09",
"EndPeriod": "2020-06-09",
"Currency": "USD",
"Option": [
{
"Name": "NoReportData",
"Value": "false"
}
]
},
"Columns": {
"Column": [
{
"ColTitle": "Date",
"ColType": "Date",
"MetaData": [
{
"Name": "ColKey",
"Value": "tx_date"
}
]
},
{
"ColTitle": "Transaction Type",
"ColType": "String",
"MetaData": [
{
"Name": "ColKey",
"Value": "txn_type"
}
]
},
{
"ColTitle": "Num",
"ColType": "String",
"MetaData": [
{
"Name": "ColKey",
"Value": "doc_num"
}
]
},
{
"ColTitle": "Name",
"ColType": "String",
"MetaData": [
{
"Name": "ColKey",
"Value": "name"
}
]
},
{
"ColTitle": "Memo/Description",
"ColType": "String",
"MetaData": [
{
"Name": "ColKey",
"Value": "memo"
}
]
},
{
"ColTitle": "Account",
"ColType": "String",
"MetaData": [
{
"Name": "ColKey",
"Value": "account_name"
}
]
},
{
"ColTitle": "Debit",
"ColType": "Money",
"MetaData": [
{
"Name": "ColKey",
"Value": "debt_home_amt"
}
]
},
{
"ColTitle": "Credit",
"ColType": "Money",
"MetaData": [
{
"Name": "ColKey",
"Value": "credit_home_amt"
}
]
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "0-00-00"
},
{
"value": "",
"id": "559"
},
{
"value": ""
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "California Department of Tax and Fee Administration Payable",
"id": "678"
},
{
"value": ""
},
{
"value": "1.25"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "0-00-00"
},
{
"value": "",
"id": "559"
},
{
"value": ""
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "California Department of Tax and Fee Administration Payable",
"id": "678"
},
{
"value": ""
},
{
"value": ".10"
}
],
"type": "Data"
},
{
"Summary": {
"ColData": [
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "31.75"
},
{
"value": "31.75"
}
]
},
"type": "Section"
},
{
"ColData": [
{
"value": "0-00-00"
},
{
"value": "",
"id": "567"
},
{
"value": ""
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "Accounts Payable (A/P)",
"id": "676"
},
{
"value": "232.00"
},
{
"value": ""
}
],
"type": "Data"
}
]
}
}
I want to filter this array which matches Rows.Row[someindex].ColData[1].id == 567. Here id should match with second object of ColData array.
But after filtering i don't want to skip the records from resulting array those have Summary as Rows.Row[someindex].Summary. Summary records should be included in resulting array.
I have tried below code but it does not work because In Row.Row array Summary can be there instead of ColData at some indexs & also i want include Summary records in resulting array even if i have found the desired object.
function getResult(filterbY, objList) {
return objList.Rows.Row.filter(function(obj) {
return obj.ColData.some(function(item){
return item.id == filterbY;
});
});
}
Please help with the same ?
you need to include your summary in your filter function then,
function getResult(filterbY, objList) {
return objList.Rows.Row.filter(function(obj) {
if(obj.Summary){return true;}
return obj.ColData.some(function(item){
return item.id == filterbY;
});
});
}

Iterate/Traverse through each child element to Add/Modify its properties

I have a scenario were need to iterate over each children element of the tree structure and modify/add properties or attributes. Each children can have multiple children
var treeStructure = {
"id": 1,
"name": "Grand Parent 1",
"children": [
{
"id": 2,
"children": [
{
"id": 3,
"children": [],
"name": "Child 11",
"properties": [
{
"id": 15,
"run": "fast"
},
{
"id": 16,
"walk": "slow"
}
]
},
{
"id": 4,
"type": "Child",
"children": [],
"name": "Child 12",
"properties": [
{
"id": 17,
"run": "slow"
},
{
"id": 18,
"walk": "medium"
}
]
}
],
"name": "Parent 1",
"properties": [
{
"id": 12,
"run": "slow"
},
{
"id": 13,
"walk": "fast"
}
]
},
{
"id": 5,
"children": [
{
"id": 6,
"children": [],
"name": "Child 21"
}
],
"name": "Parent 2",
"properties": [
{
"id": 21,
"run": "medium"
},
{
"id": 22,
"walk": "fast"
}
]
},
{
"id": 7,
"type": "Parent",
"children": [
{
"id": 8,
"children": [],
"name": "Child 31"
}
],
"name": "Parent 3",
"properties": [
{
"id": 31,
"run": "fast"
},
{
"id": 32,
"walk": "slow"
}
]
}
]
}
iterateTree(treeStructure)
iterateTree (node) {
var self = this;
function recursive(obj) {
if (obj.children && obj.children.length) {
obj.children.forEach(function(val,key){
if(val.hasOwnProperty("children")){
self.modifyProperties(val);
recursive(val.children);
}
})
}
}
var expectedOutput = recursive(node);
console.log(expectedOutput)
}
modifyProperties(nodeData) {
let thingProperties = [];
if (nodeData.properties) {
nodeData.properties.map(function (property) {
let tempObj = {
"id": null,
"actionType": "create",
"run" : property.run
};
thingProperties.push(tempObj);
})
}
return {
"id": nodeData.id,
"name": nodeData.name,
"actionType": "create",
}
}
Expected Output: I should be able to modify "id" as null and add
"actionType" as create for children and parent element as shown below
{
"id": 1,
"name": "Grand Parent 1",
"actionType": "create",
"children": [
{
"id": 2,
"actionType": "create",
"children": [
{
"id": 3,
"children": [],
"name": "Child 11",
"actionType": "create",
"properties": [
{
"id": null,
"run": "fast",
"actionType": "create",
"read": "slow"
},
{
"id": null,
"actionType": "create",
"walk": "slow",
"write": "fast"
}
]
},
{
"id": 4,
"type": "Child",
"children": [],
"name": "Child 12",
"actionType": "create",
"properties": [
{
"id": null,
"actionType": "create",
"run": "slow"
},
{
"id": null,
"actionType": "create",
"walk": "medium"
}
]
}
],
"name": "Parent 1",
"actionType": "create",
"properties": [
{
"id": null,
"actionType": "create",
"run": "slow"
},
{
"id": null,
"actionType": "create",
"walk": "fast"
}
]
},
{
"id": 5,
"children": [
{
"id": null,
"children": [],
"name": "Child 21"
}
],
"name": "Parent 2",
"actionType": "create",
"properties": [
{
"id": null,
"actionType": "create",
"run": "medium"
},
{
"id": null,
"walk": "fast"
}
]
},
{
"id": 7,
"type": "Parent",
"actionType": "create",
"children": [
{
"id": null,
"actionType": "create",
"children": [],
"name": "Child 31"
}
],
"name": "Parent 3",
"properties": [
{
"id": null,
"actionType": "create",
"run": "fast"
},
{
"id": null,
"actionType": "create",
"walk": "slow"
}
]
}
]
}

Converting an array of object - JSON

I have an array on objects like this,
[
{
"link": "link1",
"model": "model1",
"role": "role1",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role2",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role3",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role4",
"access": "true"
},
{
"link": "link1",
"model": "model2",
"role": "role1",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role2",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role3",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role4",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role1",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role2",
"access": "true"
},
{
"link": "link2",
"model": "model1",
"role": "role3",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role4",
"access": "true"
},
{
"link": "link2",
"model": "model2",
"role": "role1",
"access": "false"
},
{
"link": "link2",
"model": "model2",
"role": "role2",
"access": "true"
},
{
"link": "link2",
"model": "model2",
"role": "role3",
"access": "false"
},
{
"link": "link2",
"model": "model2",
"role": "role4",
"access": "true"
}
]
With respect to this question, the input was a CSV sample with very less number of rows. In real I have a large CSV file and I found a way to import it and convert to JSON using d3.js, but the problem is I'm unable to convert it to the desired format. I tried using the data.forEach and the output was really weird and I couldn't understand why I get model2 alone in both the links.
Code:
d3.csv('data.csv', function(data) {
var newData = {};
data.forEach(function(e, i) {
newData[e.link] = {};
newData[e.link][e.model] = {};
})
d3.select('main').append('pre')
.text(JSON.stringify(newData, null, ' '));
});
Output:
{
"link1": {
"model2": {}
},
"link2": {
"model2": {}
}
}
Desired Output:
"link1": {
"model1": {
"role1": true,
"role2": true,
"role3": true,
"role4": true,
},
"model2": {
"role1": false,
"role2": false,
"role3": false,
"role4": false,
}
},
"link2": {
"model1": {
"role1": false,
"role2": true,
"role3": false,
"role4": true,
},
"model2": {
"role1": false,
"role2": true,
"role3": false,
"role4": true,
}
}
Any help will be much appreciated. Thanks in advance.
In your forEach, you're not assigning the role and access pair on each iteration, and you're always emptying each section.
Change your forEach with this...
data.forEach(function(e, i) {
// Check if this "link" already exists exists, if not, create it.
if (!newData[e.link])
newData[e.link] = {};
// Check if this "model" already exists inside of this "link", if not, create it.
if (!newData[e.link][e.model])
newData[e.link][e.model] = {};
newData[e.link][e.model][e.role] = e.access;
});
I hope it helps

Kendo Treeview of checkboxes not working in angular js

I am implementing kendo treeview of checkboxes in angular. I get no error, but tree is not rendering. My code is here:
addCtrl.js
$scope.updateTree = function () {
websiteService.getWebsiteFeaturesTree(webName, function (data) {
$scope.treeOptions = {
checkboxes: {
checkChildren: true
}
};
$scope.treeDataSource = data;
});
}
add.html
<div kendo-tree-view k-options="treeOptions" k-data-source="treeDataSource"></div>
app.js
var app = angular.module('app', ['ui.bootstrap', 'ngRoute', 'kendo.directives']);
I get datasource from webservice. here is json
// 20160930204148
// http://localhost/api/Website/GetWebsiteFeaturesTree?webName=MetisEmptyTemplate
[
{
"Name": "Section",
"text": "Admin",
"checked": false,
"items": [
{
"Name": "Tab",
"text": "Matrix Analysis",
"checked": true,
"items": [
]
},
{
"Name": "Tab",
"text": "Risk Categories and Questions",
"checked": true,
"items": [
]
},
{
"Name": "Tab",
"text": "Risk Colors",
"checked": true,
"items": [
]
},
{
"Name": "Tab",
"text": "SWOT Types",
"checked": false,
"items": [
]
}
]
},
{
"Name": "Section",
"text": "Strategy",
"checked": false,
"items": [
{
"Name": "Tab",
"text": "SWOT Analysis",
"checked": false,
"items": [
{
"Name": "Grid",
"text": "SWOT Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": false,
"items": [
{
"Name": "Output",
"text": "SWOT Box",
"checked": false,
"items": [
]
}
]
}
]
}
]
},
{
"Name": "Section",
"text": "Portfolio",
"checked": true,
"items": [
{
"Name": "Tab",
"text": "Matrix",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Matrix Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Matrix Output",
"checked": true,
"items": [
]
}
]
}
]
},
{
"Name": "Tab",
"text": "Strategy",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Strategy Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Investment Level by Objective",
"checked": true,
"items": [
]
}
]
}
]
},
{
"Name": "Tab",
"text": "Risk",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Risk Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Portfolio Risk",
"checked": true,
"items": [
]
},
{
"Name": "Output",
"text": "Portfolio Risk Stacked Bar",
"checked": true,
"items": [
]
}
]
}
]
}
]
},
{
"Name": "Section",
"text": "Project",
"checked": false,
"items": [
{
"Name": "Tab",
"text": "Matrix",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Matrix Grid",
"checked": true,
"items": [
]
}
]
},
{
"Name": "Tab",
"text": "Strategy",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Strategy Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Project Tactic Linkage",
"checked": true,
"items": [
]
},
{
"Name": "Output",
"text": "OGTM",
"checked": true,
"items": [
]
}
]
}
]
},
{
"Name": "Tab",
"text": "Risk",
"checked": false,
"items": [
{
"Name": "Grid",
"text": "Risk Grid",
"checked": false,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Risk Charts",
"checked": true,
"items": [
]
}
]
}
]
}
]
}
]
Although my data source is correct. What thing is missing in it? I am not getting any error. My tree is not rendering in html.
Try placing your data in a new kendo.data.HierarchicalDataSource

Traverse Object and build Tree Lineage

Json Object :
var json = {
"Tree": [{
"Title": "Condition",
"Attr": {
"Id": 2258,
"Zone": null
},
"Children": [{
"Title": "General Wellness",
"Attr": {
"Id": 2315,
"Zone": null
},
"Children": [{
"Title": "Family Health",
"Attr": {
"Id": 2262,
"Zone": null
},
"Children": []
}, {
"Title": "Healthy Home",
"Attr": {
"Id": 2316,
"Zone": null
},
"Children": []
}, {
"Title": "Vitamins",
"Attr": {
"Id": 2317,
"Zone": null
},
"Children": []
}, {
"Title": "Recipes",
"Attr": {
"Id": 2318,
"Zone": null
},
"Children": []
}, {
"Title": "Caregiving",
"Attr": {
"Id": 2325,
"Zone": null
},
"Children": []
}, {
"Title": "Healthy Eating",
"Attr": {
"Id": 2346,
"Zone": null
},
"Children": []
}, {
"Title": "Travel Health",
"Attr": {
"Id": 2347,
"Zone": null
},
"Children": []
}]
}]
}]
}
I am able to traverse the tree however I am unable to build a tree structure for example if I wanted to look for "Recipes" it should return me the result as :
Condition > General Wellness > Recipes
UPDATE :
Traversing is done via :
function process(key,value) {
alert(key + " : "+value);
}
function traverse(o,func) {
for (var i in o) {
func.apply(this,[i,o[i]]);
if (o[i] !== null && typeof(o[i])=="object") {
traverse(o[i],func);
}
}
}
traverse(json,process);
I suggest an iterative, as you already had, approach with a defined exit if found and a repeated calling if not found. The path is created if the target is found.
Bonus: The getPath returns the success of the search.
function getPath(array, target, path) {
return array.some(function (a) {
if (a.Title === target) {
return path.unshift(a.Title);
} else if (Array.isArray(a.Children)) {
return getPath(a.Children, target, path) && path.unshift(a.Title);
}
});
}
var data = {
"Tree": [{
"Title": "Condition",
"Attr": {
"Id": 2258,
"Zone": null
},
"Children": [{
"Title": "General Wellness",
"Attr": {
"Id": 2315,
"Zone": null
},
"Children": [{
"Title": "Family Health",
"Attr": {
"Id": 2262,
"Zone": null
},
"Children": []
}, {
"Title": "Healthy Home",
"Attr": {
"Id": 2316,
"Zone": null
},
"Children": []
}, {
"Title": "Vitamins",
"Attr": {
"Id": 2317,
"Zone": null
},
"Children": []
}, {
"Title": "Recipes",
"Attr": {
"Id": 2318,
"Zone": null
},
"Children": []
}, {
"Title": "Caregiving",
"Attr": {
"Id": 2325,
"Zone": null
},
"Children": []
}, {
"Title": "Healthy Eating",
"Attr": {
"Id": 2346,
"Zone": null
},
"Children": []
}, {
"Title": "Travel Health",
"Attr": {
"Id": 2347,
"Zone": null
},
"Children": []
}]
}]
}, {
Title: 'abc',
Children: [{
Title: 'def',
Children: [{
Title: 'ghi'
}]
}, {
Title: 'jkl',
Children: [{
Title: 'mno'
}]
}]
}]
},
path = [];
document.write(getPath(data.Tree, 'Recipes', path) + ': ' + path.join(' > ') + '<br>');
path = [];
document.write(getPath(data.Tree, 'ghi', path) + ': ' + path.join(' > ') + '<br>');
path = [];
document.write(getPath(data.Tree, 'nonsense', path) + ': ' + path.join(' > ') + '<br>');

Categories

Resources