Access object properties - javascript

{
id: '1',
type: "folder",
name: "new Folder 1",
files: [
{ id: '1.1', type: "file", name: "file 1" },
{ id: '1.2', type: "file", name: "file 2" },
{ id: '1.3', type: "file", name: "file 3" },
]
},
{
id: '2',
type: "folder",
name: "new Folder 2",
files: [
{ id: '2.1', type: "file", name: "file 4" },
{
id: '2.2',
type: "folder",
name: "new Folder 3",
files: [
{ id: '2.2.1', type: "file", name: "file 7" },
]
},
{ id: '2.3', type: "file", name: "file 5" },
{ id: '2.4', type: "file", name: "file 6" },
]
},
i want to access and pushing some elements to this kinda of data structure like pushing some data or crating folders under list[1][1][2] or to list[1] but i dont know the number of the iterations it can be 1 or more its variable

Related

How to I achieve nesting for objects having ParentKey?

I have an array of objects with Name, Key in all objects and ParentKey in some of the objects.
I want to group objects by unique 'Key' excluding those which have 'ParentKey'. The objects having ParentKey should be nested as reflected in required ans.
Initial Input :-
const arr = [
{
Name: 'Manage Leads',
Key: 'Manage Leads',
},
{
Name: 'Dashboard',
Key: 'Dashboard',
},
{
Name: 'Smart Views',
Key: 'Smart Views',
},
{
Name: 'Lead Details',
Key: 'Lead Details',
},
{
Name: 'Opportunity Details',
Key: 'Opportunity Details',
},
{
Name: 'Header',
Key: 'Header',
},
{
Name: 'Settings',
Key: 'Settings',
},
{
Name: 'Test 1',
Key: 'Test1Key',
},
{
Name: 'Test 1.1',
Key: 'Test1.1Key',
ParentKey: 'Test1',
},
{
Name: 'Test 1.2',
Key: 'Test1.2Key',
ParentKey: 'Test1',
},
{
Name: 'Test 1.1.1',
Key: 'Test1.1.1Key',
ParentKey: 'Test 1.1',
},
{
Name: 'Test 1.1.2',
Key: 'Test1.1.2Key',
ParentKey: 'Test 1.1',
},
{
Name: 'Test 1.2.1',
Key: 'Test1.2.1Key',
ParentKey: 'Test 1.2',
},
];
Required Output :-
[
{
groupName: 'Manage Leads',
actionsArr: [{ Name: 'Manage Leads', Key: 'Manage Leads' }],
},
{
groupName: 'Dashboard',
actionsArr: [{ Name: 'Dashboard', Key: 'Dashboard' }],
},
{
groupName: 'Smart Views',
actionsArr: [{ Name: 'Smart Views', Key: 'Smart Views' }],
},
{
groupName: 'Lead Details',
actionsArr: [{ Name: 'Lead Details', Key: 'Lead Details' }],
},
{
groupName: 'Opportunity Details',
actionsArr: [
{
Name: 'Opportunity Details',
Key: 'Opportunity Details',
},
],
},
{
groupName: 'Header',
actionsArr: [{ Name: 'Header', Key: 'Header' }],
},
{
groupName: 'Settings',
actionsArr: [{ Name: 'Settings', Key: 'Settings' }],
},
{
groupName: 'Test1Key',
actionsArr: [
{
Name: 'Test 1',
Key: 'Test1Key',
subActions: [
{
Name: 'Test 1.1',
Key: 'Test1.1',
ParentKey: 'Test1Key',
subActions: [
{
Name: 'Test 1.1.1',
Key: 'Test1.1.1Key',
ParentKey: 'Test 1.1',
},
{
Name: 'Test 1.1.2',
Key: 'Test1.1.2Key',
ParentKey: 'Test 1.1',
},
],
},
{
Name: 'Test 1.2',
Key: 'Test1.2',
ParentKey: 'Test1Key',
subActions: [
{
Name: 'Test 1.2.1',
Key: 'Test1.2.1Key',
ParentKey: 'Test 1.2',
},
],
},
],
},
],
},
];
I was able to group by unique Key with the follwoing code but i'm not able to nest the objects which have ParentKey.
const groupNames = [...new Set(actions.map((item) => item.GroupKey))];
const actionsList = groupNames.map((groupName) => {
const actionsArr = actions.filter((act) => act.GroupKey === groupName);
return { label: groupName, value: actionsArr }});
I found some inconsistencies between Key and ParentKey for the given data.
I have fixed the inconsistencies in the input data, and I would be using the same.
Here is the solution that worked for me.
const arr = [
{
Name: "Manage Leads",
Key: "Manage Leads",
},
{
Name: "Dashboard",
Key: "Dashboard",
},
{
Name: "Smart Views",
Key: "Smart Views",
},
{
Name: "Lead Details",
Key: "Lead Details",
},
{
Name: "Opportunity Details",
Key: "Opportunity Details",
},
{
Name: "Header",
Key: "Header",
},
{
Name: "Settings",
Key: "Settings",
},
{
Name: "Test 1",
Key: "Test1Key",
},
{
Name: "Test 1.1",
Key: "Test1.1Key",
ParentKey: "Test 1",
},
{
Name: "Test 1.2",
Key: "Test1.2Key",
ParentKey: "Test 1",
},
{
Name: "Test 1.1.1",
Key: "Test1.1.1Key",
ParentKey: "Test 1.1",
},
{
Name: "Test 1.1.2",
Key: "Test1.1.2Key",
ParentKey: "Test 1.1",
},
{
Name: "Test 1.2.1",
Key: "Test1.2.1Key",
ParentKey: "Test 1.2",
},
];
//recursively look for the child and append child to parent
const appendChild = (parent, arr) => {
for (let index = 0; index < arr.length; index++) {
const childElement = arr[index];
if (childElement.ParentKey == parent.Name) {
appendChild(childElement, arr);
if (parent.subActions) {
parent.subActions.push(childElement);
} else {
parent.subActions = [childElement];
}
//remove the child from that list and match the index
arr.splice(index, 1);
index--;
}
}
};
//convert data into a parent-child hierarchy
const convert = (arr) => {
for (const parent of arr) {
appendChild(parent, arr);
}
//filter out items which has been already added as child/subActions
return arr.filter((x) => !Boolean(x.ParentKey));
};
const groupBy = (items, callback) => {
const groupedData = items.reduce(
(acc, value, index) => (
(acc[callback(value, index, items)] ||= []).push(value), acc
),
{}
);
//convert grouped data as the required output
return Object.entries(groupedData).map(([key, value]) => ({
groupName: key,
actionsArr: value,
}));
};
const data = groupBy(convert(arr), (x) => x.Name);
console.log(JSON.stringify(data, null, 2));
Output:
[
{
"groupName": "Manage Leads",
"actionsArr": [
{
"Name": "Manage Leads",
"Key": "Manage Leads"
}
]
},
{
"groupName": "Dashboard",
"actionsArr": [
{
"Name": "Dashboard",
"Key": "Dashboard"
}
]
},
{
"groupName": "Smart Views",
"actionsArr": [
{
"Name": "Smart Views",
"Key": "Smart Views"
}
]
},
{
"groupName": "Lead Details",
"actionsArr": [
{
"Name": "Lead Details",
"Key": "Lead Details"
}
]
},
{
"groupName": "Opportunity Details",
"actionsArr": [
{
"Name": "Opportunity Details",
"Key": "Opportunity Details"
}
]
},
{
"groupName": "Header",
"actionsArr": [
{
"Name": "Header",
"Key": "Header"
}
]
},
{
"groupName": "Settings",
"actionsArr": [
{
"Name": "Settings",
"Key": "Settings"
}
]
},
{
"groupName": "Test 1",
"actionsArr": [
{
"Name": "Test 1",
"Key": "Test1Key",
"subActions": [
{
"Name": "Test 1.1",
"Key": "Test1.1Key",
"ParentKey": "Test 1",
"subActions": [
{
"Name": "Test 1.1.1",
"Key": "Test1.1.1Key",
"ParentKey": "Test 1.1"
},
{
"Name": "Test 1.1.2",
"Key": "Test1.1.2Key",
"ParentKey": "Test 1.1"
}
]
},
{
"Name": "Test 1.2",
"Key": "Test1.2Key",
"ParentKey": "Test 1",
"subActions": [
{
"Name": "Test 1.2.1",
"Key": "Test1.2.1Key",
"ParentKey": "Test 1.2"
}
]
}
]
}
]
}
]

Flat an array tree js

Can you help me please to flat this tree?
I have tried a few things and it didn't work.
I would like to get the fastest way(Algorithm).
const source = [
{
item: { id: 1, name: "item name", code: "1d4g4" },
children: [
{
item: { id: 2, name: "item name 2", code: "1d4g4" },
children: [
{
item: { id: 2, name: "item name 2", code: "1d4g4" },
children: [
{
item: { id: 3, name: "item name 2", code: "1d4g4" },
children: [
{ item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] },
{ item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] },
{ item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] },
],
},
],
},
],
},
],
},
];
This is result that i expect to have:
{ id: 1, name: 'item name', code: '1d4g4' },
{ id: 2, name: 'item name 2', code: '1d4g4' },
{ id: 2, name: 'item name 2', code: '1d4g4' },
{ id: 3, name: 'item name 2', code: '1d4g4' },
{ id: 4, name: 'item name 2', code: '1d4g4' },
{ id: 4, name: 'item name 2', code: '1d4g4' },
{ id: 4, name: 'item name 2', code: '1d4g4' }
]```
You could take Array#flatMap and a callback which calls itself.
const
flat = ({ item, children = [] }) => [item, ...children.flatMap(flat)],
data = [{ item: { id: 1, name: "item name", code: "1d4g4" }, children: [{ item: { id: 2, name: "item name 2", code: "1d4g4" }, children: [{ item: { id: 2, name: "item name 2", code: "1d4g4" }, children: [{ item: { id: 3, name: "item name 2", code: "1d4g4" }, children: [{ item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] }, { item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] }, { item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] }] }] }] }] }],
result = data.flatMap(flat);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
After fixing your syntax to be actually valid JavaScript, you'll need a recursive function:
function flatten(destArray, nodeList) {
nodeList.forEach((node) => {
destArray.push(node.item);
flatten(destArray, node.children || []);
});
}
const source = [
{
item: { id: 1, name: "item name", code: "1d4g4" },
children: [
{
item: { id: 2, name: "item name 2", code: "1d4g4" },
children: [
{
item: { id: 2, name: "item name 2", code: "1d4g4" },
children: [
{
item: { id: 3, name: "item name 2", code: "1d4g4" },
children: [
{ item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] },
{ item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] },
{ item: { id: 4, name: "item name 2", code: "1d4g4" }, children: [] },
],
},
],
},
],
},
],
},
];
const dest = [];
flatten(dest, source);
console.log(dest);
outputs
[
{ id: 1, name: 'item name', code: '1d4g4' },
{ id: 2, name: 'item name 2', code: '1d4g4' },
{ id: 2, name: 'item name 2', code: '1d4g4' },
{ id: 3, name: 'item name 2', code: '1d4g4' },
{ id: 4, name: 'item name 2', code: '1d4g4' },
{ id: 4, name: 'item name 2', code: '1d4g4' },
{ id: 4, name: 'item name 2', code: '1d4g4' }
]
You could write an internal visit method to handle traversing the tree and adding items to an internal results list.
Note: Make sure your JS/JSON data is structured correctly.
const tree = [{
item: {id: 1, name: "item name", code: "1d4g4"},
children: [{
item: {id: 2, name: "item name 2", code: "1d4g4"},
children: [{
item: {id: 2, name: "item name 2", code: "1d4g4"},
children: [{
item: {id: 3, name: "item name 2", code: "1d4g4"},
children:[
{item: {id: 4, name: "item name 2", code: "1d4g4"}, children: []},
{item: {id: 4, name: "item name 2", code: "1d4g4"}, children: []},
{item: {id: 4, name: "item name 2", code: "1d4g4"}, children: []},
]
}]
}]
}]
}];
const treeToList = (tree, results = []) => {
const visit = ({ item, children = [] }, res) => {
if (item) res.push(item);
children.forEach(child => visit(child, res));
}
visit({ children: tree }, results);
return results;
}
console.log(treeToList(tree));
.as-console-wrapper { top: 0; max-height: 100% !important; }
const flatten = (data) => data.map(({ children }) => ([...flatten(children)]));
Your current code does traverse the whole tree, but never actually extracts item from the data. Another issue is that you currently use map, which means the resulting value will always have the same amount of elements as the initial array. Use flatMap to increase or reduce the amount of elements in the array.
Changing your code as little as possible it might look like this:
const flatten = (data) => data.flatMap(({item, children}) => ([item, ...flatten(children)]));
const flatten = (data) => data.flatMap(({item, children}) => ([item, ...flatten(children)]));
const data = [{
item: {id: 1, name: "item name", code: "1d4g4"},
children: [{
item: {id: 2, name: "item name 2", code: "1d4g4"},
children: [{
item: {id: 2, name: "item name 2", code: "1d4g4"},
children: [{
item: {id: 3, name: "item name 2", code: "1d4g4"},
children:[
{item: {id: 4, name: "item name 2", code: "1d4g4"}, children: []},
{item: {id: 4, name: "item name 2", code: "1d4g4"}, children: []},
{item: {id: 4, name: "item name 2", code: "1d4g4"}, children: []},
]
}]
}]
}]
}];
console.log(flatten(data));

Best -and most efficient- way to programmatically destructure specific properties from a HUGE JSON of nested objects and arrays

So because I'm working with a SOAP API that returns XML and I need/want JSON after I xml2json the response this is exactly what I get: a Ticket!
What I need is for example:
the ticket description
the ticket title
the ticket number
...some more but 3 for demonstration purposes is enough
Being the JSON object below so nasty...what is the best or most efficient way to get what I need above:
-ticket description
-ticket title
-ticket number
You can find in the JSON the objects which names are:
Description, Title, Ticket Number (all strings)
I've thought about making it an array or arrays using Object.entries(JSON)
then finding how deep this array of arrays is using a recursive function.
Maybe then trying to flatten the arrays of arrays to whatever depth was found above.
But I still keep having mixed arrays and objects.
HELP!
const flattened = [
"declaration",
{ attributes: { version: "1.0", encoding: "utf-8" } },
"elements",
{
type: "element",
name: "soap:Envelope",
attributes: {
"xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
},
elements: [
{
type: "element",
name: "soap:Body",
elements: [
{
type: "element",
name: "queryResponse",
attributes: { xmlns: "http://autotask.net/ATWS/v1_6/" },
elements: [
{
type: "element",
name: "queryResult",
elements: [
{
type: "element",
name: "ReturnCode",
elements: [{ type: "text", text: "1" }]
},
{
type: "element",
name: "EntityResults",
elements: [
{
type: "element",
name: "Entity",
attributes: { "xsi:type": "Ticket" },
elements: [
{
type: "element",
name: "id",
elements: [{ type: "text", text: "40218" }]
},
{
type: "element",
name: "UserDefinedFields",
elements: [
{
type: "element",
name: "UserDefinedField",
elements: [
{
type: "element",
name: "Name",
elements: [
{ type: "text", text: "Kaseya Alert ID" }
]
}
]
},
{
type: "element",
name: "UserDefinedField",
elements: [
{
type: "element",
name: "Name",
elements: [
{ type: "text", text: "Kaseya Ticket ID" }
]
}
]
},
{
type: "element",
name: "UserDefinedField",
elements: [
{
type: "element",
name: "Name",
elements: [
{ type: "text", text: "Logicalis ticket" }
]
},
{
type: "element",
name: "Value",
elements: [{ type: "text", text: "yes" }]
}
]
}
]
},
{
type: "element",
name: "AccountID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "0" }]
},
{
type: "element",
name: "AllocationCodeID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "29682804" }]
},
{
type: "element",
name: "CreateDate",
attributes: { "xsi:type": "xsd:dateTime" },
elements: [
{ type: "text", text: "2019-09-24T13:56:45.47" }
]
},
{
type: "element",
name: "CreatorResourceID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "29682933" }]
},
{
type: "element",
name: "Description",
attributes: { "xsi:type": "xsd:string" },
elements: [
{
type: "text",
text:
"my description"
}
]
},
{
type: "element",
name: "DueDateTime",
attributes: { "xsi:type": "xsd:dateTime" },
elements: [
{ type: "text", text: "2019-09-24T14:56:45.47" }
]
},
{
type: "element",
name: "EstimatedHours",
attributes: { "xsi:type": "xsd:decimal" },
elements: [{ type: "text", text: "0.0000" }]
},
{
type: "element",
name: "IssueType",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "12" }]
},
{
type: "element",
name: "LastActivityDate",
attributes: { "xsi:type": "xsd:dateTime" },
elements: [
{ type: "text", text: "2019-10-14T11:12:59.337" }
]
},
{
type: "element",
name: "Priority",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "1" }]
},
{
type: "element",
name: "QueueID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "29682969" }]
},
{
type: "element",
name: "AssignedResourceID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "29682919" }]
},
{
type: "element",
name: "AssignedResourceRoleID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "29683464" }]
},
{
type: "element",
name: "Source",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "4" }]
},
{
type: "element",
name: "Status",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "1" }]
},
{
type: "element",
name: "SubIssueType",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "146" }]
},
{
type: "element",
name: "TicketNumber",
attributes: { "xsi:type": "xsd:string" },
elements: [{ type: "text", text: "T20190924.0157" }]
},
{
type: "element",
name: "Title",
attributes: { "xsi:type": "xsd:string" },
elements: [
{
type: "text",
text:
"enabling file share/external sharing in Microsoft Teams"
}
]
},
{
type: "element",
name: "FirstResponseDueDateTime",
attributes: { "xsi:type": "xsd:dateTime" },
elements: [
{ type: "text", text: "2019-09-24T14:26:45.47" }
]
},
{
type: "element",
name: "ResolvedDueDateTime",
attributes: { "xsi:type": "xsd:dateTime" },
elements: [
{ type: "text", text: "2019-09-24T14:56:45.47" }
]
},
{
type: "element",
name: "ServiceLevelAgreementID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "2" }]
},
{
type: "element",
name: "Resolution",
attributes: { "xsi:type": "xsd:string" }
},
{
type: "element",
name: "PurchaseOrderNumber",
attributes: { "xsi:type": "xsd:string" }
},
{
type: "element",
name: "TicketType",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "1" }]
},
{
type: "element",
name: "ChangeInfoField1",
attributes: { "xsi:type": "xsd:string" }
},
{
type: "element",
name: "ChangeInfoField2",
attributes: { "xsi:type": "xsd:string" }
},
{
type: "element",
name: "ChangeInfoField3",
attributes: { "xsi:type": "xsd:string" }
},
{
type: "element",
name: "ChangeInfoField4",
attributes: { "xsi:type": "xsd:string" }
},
{
type: "element",
name: "ChangeInfoField5",
attributes: { "xsi:type": "xsd:string" }
},
{
type: "element",
name: "LastCustomerVisibleActivityDateTime",
attributes: { "xsi:type": "xsd:dateTime" },
elements: [
{ type: "text", text: "2019-10-14T11:12:59.273" }
]
},
{
type: "element",
name: "HoursToBeScheduled",
attributes: { "xsi:type": "xsd:decimal" },
elements: [{ type: "text", text: "0.0000" }]
},
{
type: "element",
name: "TicketCategory",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "3" }]
},
{
type: "element",
name: "ExternalID",
attributes: { "xsi:type": "xsd:string" }
},
{
type: "element",
name: "CreatorType",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "1" }]
},
{
type: "element",
name: "AccountPhysicalLocationID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "1" }]
},
{
type: "element",
name: "LastActivityPersonType",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "1" }]
},
{
type: "element",
name: "LastActivityResourceID",
attributes: { "xsi:type": "xsd:int" },
elements: [{ type: "text", text: "29682919" }]
}
]
}
]
},
{
type: "element",
name: "EntityResultType",
elements: [{ type: "text", text: "ticket" }]
},
{ type: "element", name: "Errors" },
{
type: "element",
name: "EntityReturnInfoResults",
elements: [
{
type: "element",
name: "EntityReturnInfo",
elements: [
{
type: "element",
name: "EntityId",
elements: [{ type: "text", text: "40218" }]
},
{
type: "element",
name: "DatabaseAction",
elements: [{ type: "text", text: "None" }]
},
{
type: "element",
name: "DuplicateStatus",
elements: [
{
type: "element",
name: "Found",
elements: [{ type: "text", text: "false" }]
},
{ type: "element", name: "MatchInfo" },
{
type: "element",
name: "Ignored",
elements: [{ type: "text", text: "false" }]
}
]
},
{ type: "element", name: "Message" }
]
}
]
}
]
}
]
}
]
}
]
}
];
You could create a function which recursively goes through an object and it's elements. If the name is included in the set of names, add that to the output array
const flattened = ["declaration",{attributes:{version:"1.0",encoding:"utf-8"}},"elements",{type:"element",name:"soap:Envelope",attributes:{"xmlns:soap":"http://schemas.xmlsoap.org/soap/envelope/","xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","xmlns:xsd":"http://www.w3.org/2001/XMLSchema"},elements:[{type:"element",name:"soap:Body",elements:[{type:"element",name:"queryResponse",attributes:{xmlns:"http://autotask.net/ATWS/v1_6/"},elements:[{type:"element",name:"queryResult",elements:[{type:"element",name:"ReturnCode",elements:[{type:"text",text:"1"}]},{type:"element",name:"EntityResults",elements:[{type:"element",name:"Entity",attributes:{"xsi:type":"Ticket"},elements:[{type:"element",name:"id",elements:[{type:"text",text:"40218"}]},{type:"element",name:"UserDefinedFields",elements:[{type:"element",name:"UserDefinedField",elements:[{type:"element",name:"Name",elements:[{type:"text",text:"Kaseya Alert ID"}]}]},{type:"element",name:"UserDefinedField",elements:[{type:"element",name:"Name",elements:[{type:"text",text:"Kaseya Ticket ID"}]}]},{type:"element",name:"UserDefinedField",elements:[{type:"element",name:"Name",elements:[{type:"text",text:"Logicalis ticket"}]},{type:"element",name:"Value",elements:[{type:"text",text:"yes"}]}]}]},{type:"element",name:"AccountID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"0"}]},{type:"element",name:"AllocationCodeID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682804"}]},{type:"element",name:"CreateDate",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-09-24T13:56:45.47"}]},{type:"element",name:"CreatorResourceID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682933"}]},{type:"element",name:"Description",attributes:{"xsi:type":"xsd:string"},elements:[{type:"text",text:"my description"}]},{type:"element",name:"DueDateTime",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-09-24T14:56:45.47"}]},{type:"element",name:"EstimatedHours",attributes:{"xsi:type":"xsd:decimal"},elements:[{type:"text",text:"0.0000"}]},{type:"element",name:"IssueType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"12"}]},{type:"element",name:"LastActivityDate",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-10-14T11:12:59.337"}]},{type:"element",name:"Priority",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"QueueID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682969"}]},{type:"element",name:"AssignedResourceID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682919"}]},{type:"element",name:"AssignedResourceRoleID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29683464"}]},{type:"element",name:"Source",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"4"}]},{type:"element",name:"Status",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"SubIssueType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"146"}]},{type:"element",name:"TicketNumber",attributes:{"xsi:type":"xsd:string"},elements:[{type:"text",text:"T20190924.0157"}]},{type:"element",name:"Title",attributes:{"xsi:type":"xsd:string"},elements:[{type:"text",text:"enabling file share/external sharing in Microsoft Teams"}]},{type:"element",name:"FirstResponseDueDateTime",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-09-24T14:26:45.47"}]},{type:"element",name:"ResolvedDueDateTime",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-09-24T14:56:45.47"}]},{type:"element",name:"ServiceLevelAgreementID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"2"}]},{type:"element",name:"Resolution",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"PurchaseOrderNumber",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"TicketType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"ChangeInfoField1",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"ChangeInfoField2",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"ChangeInfoField3",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"ChangeInfoField4",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"ChangeInfoField5",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"LastCustomerVisibleActivityDateTime",attributes:{"xsi:type":"xsd:dateTime"},elements:[{type:"text",text:"2019-10-14T11:12:59.273"}]},{type:"element",name:"HoursToBeScheduled",attributes:{"xsi:type":"xsd:decimal"},elements:[{type:"text",text:"0.0000"}]},{type:"element",name:"TicketCategory",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"3"}]},{type:"element",name:"ExternalID",attributes:{"xsi:type":"xsd:string"}},{type:"element",name:"CreatorType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"AccountPhysicalLocationID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"LastActivityPersonType",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"1"}]},{type:"element",name:"LastActivityResourceID",attributes:{"xsi:type":"xsd:int"},elements:[{type:"text",text:"29682919"}]}]}]},{type:"element",name:"EntityResultType",elements:[{type:"text",text:"ticket"}]},{type:"element",name:"Errors"},{type:"element",name:"EntityReturnInfoResults",elements:[{type:"element",name:"EntityReturnInfo",elements:[{type:"element",name:"EntityId",elements:[{type:"text",text:"40218"}]},{type:"element",name:"DatabaseAction",elements:[{type:"text",text:"None"}]},{type:"element",name:"DuplicateStatus",elements:[{type:"element",name:"Found",elements:[{type:"text",text:"false"}]},{type:"element",name:"MatchInfo"},{type:"element",name:"Ignored",elements:[{type:"text",text:"false"}]}]},{type:"element",name:"Message"}]}]}]}]}]}]}],
input = flattened[3], // get the object from index 3
names = new Set(['Description', 'Title', 'TicketNumber']);
function filter(o, names) {
const output = [];
if (names.has(o.name))
output.push(o)
if (o.elements)
output.push(...o.elements.flatMap(e => filter(e, names)))
return output
}
console.log(filter(input, names))

Delete One object From Dynamically created ObjectArray

I have a object array
list Format like this
var myFormData = [
{
id: 1,
name: "first name",
type: "test",
root: "/myfolder"
},
{
id: 3,
name: "your name",
type: "test 2",
root: "/myfolder2"
}, {
id: 4,
name: "your test",
type: "test 3",
root: "/myfold",
child: [
{
id: 5,
name: "name",
type: "testf",
root: "/myfoldertr"
},
{
id: 6,
name: "first-name",
type: "test",
root: "/myfolderoot",
child: [
{
id: 8,
name: "sub first name",
type: "test5",
root: "/myfoldertest"
}, {
id: 9,
name: "first name root",
type: "test9",
root: "/myfolder",
child: [
{
id: 10,
name: "normal first name",
type: "test5",
root: "/myfoldertest"
}, {
id: 11,
name: "last first name",
type: "test5",
root: "/myfoldertest"
}
]
},
{
id: 12,
name: "name Name",
type: "testf",
root: "/myfoldertr"
}
]
},
{
id: 7,
name: "first name",
type: "test",
root: "/myfolder"
}
]
}]
This format is created with database so i cant conform that the datas are exact.Some times they have child or not.
I want to delete one object if id is equal to given id (get from programatically) Eg: i want to delete id=11.
The key point is that you have to look deep into the target array. This code snippet sample used the recursive call to deep into the nested array.
function deleteObj(target, id) {
if (!Array.isArray(target)) return;
target.forEach(function(item, index) {
if (item.child) {
target = deleteObj(item.child, id);
}
if (item.id === 11) {
target.splice(index, 1);
}
});
}
var myFormData = [{
id: 1,
name: "first name",
type: "test",
root: "/myfolder"
},
{
id: 3,
name: "your name",
type: "test 2",
root: "/myfolder2"
}, {
id: 4,
name: "your test",
type: "test 3",
root: "/myfold",
child: [{
id: 5,
name: "name",
type: "testf",
root: "/myfoldertr"
},
{
id: 6,
name: "first-name",
type: "test",
root: "/myfolderoot",
child: [{
id: 8,
name: "sub first name",
type: "test5",
root: "/myfoldertest"
}, {
id: 9,
name: "first name root",
type: "test9",
root: "/myfolder",
child: [{
id: 10,
name: "normal first name",
type: "test5",
root: "/myfoldertest"
}, {
id: 11,
name: "last first name",
type: "test5",
root: "/myfoldertest"
}]
},
{
id: 12,
name: "name Name",
type: "testf",
root: "/myfoldertr"
}
]
},
{
id: 7,
name: "first name",
type: "test",
root: "/myfolder"
}
]
}
];
function deleteObj(target, id) {
if (!Array.isArray(target)) return;
target.forEach(function(item, index) {
if (item.child) {
target = deleteObj(item.child, id);
}
if (item.id === 11) {
target.splice(index, 1);
}
});
}
deleteObj(myFormData, 11);
console.log(myFormData);

validate json array schema

guys i have this json
var menu = [{
name: 'Computers',
children: [{
name: 'Notebook'
children: [{
name: 'Apple'
}, {
name: 'Windows'
}]
}, {
name: Tablets
children: [{
name: 'Apple'
}, {
name: 'Android'
}, {
name: 'Windows'
}]
}]
}, {
name: 'Phones',
children: [{
name: 'Android'
children: [{
name: 'Samsung'
}, {
name: 'Nokia'
}, {
name: 'Lenovo'
}]
}, {
name: 'Windows Phones'
children: [{
name: 'Microsoft'
}, {
name: 'Nokia'
}]
}]
}, {
name: 'Cameras',
children: [{
name: 'Digital'
children: [{
name: 'Nikon'
}, {
name: 'Fuji'
}]
}, {
name: 'DSLR'
children: [{
name: 'Canon'
}, {
name: 'Nikon'
}]
}]
}];
it says it is not valid json ... so how to make it valid json ??
any help would be appreciated ... thanks a lot
btw i am beginner so please help me
any suggestions ?? thanks again :)
You have some missing commas, where noted and a suposed to be a string without delimiters in your object literal.
var menu = [{
name: 'Computers',
children: [{
name: 'Notebook', // missing ,
children: [{
name: 'Apple'
}, {
name: 'Windows'
}]
}, {
name: 'Tablets', // missing string delimiter and comma
children: [{
name: 'Apple'
}, {
name: 'Android'
}, {
name: 'Windows'
}]
}]
}, {
name: 'Phones',
children: [{
name: 'Android', // missing ,
children: [{
name: 'Samsung'
}, {
name: 'Nokia'
}, {
name: 'Lenovo'
}]
}, {
name: 'Windows Phones', // missing ,
children: [{
name: 'Microsoft'
}, {
name: 'Nokia'
}]
}]
}, {
name: 'Cameras',
children: [{
name: 'Digital', // missing ,
children: [{
name: 'Nikon'
}, {
name: 'Fuji'
}]
}, {
name: 'DSLR', // missing ,
children: [{
name: 'Canon'
}, {
name: 'Nikon'
}]
}]
}];
console.log(menu);
.as-console-wrapper { max-height: 100% !important; top: 0; }
There are two problems
After name property values, comma is missing (at various places)
Tablets is not in quotes
Correct syntax would be.
var menu = [{
name: 'Computers',
children: [{
name: 'Notebook',
children: [{
name: 'Apple'
}, {
name: 'Windows'
}]
}, {
name: 'Tablets',
children: [{
name: 'Apple'
}, {
name: 'Android'
}, {
name: 'Windows'
}]
}]
}, {
name: 'Phones',
children: [{
name: 'Android',
children: [{
name: 'Samsung'
}, {
name: 'Nokia'
}, {
name: 'Lenovo'
}]
}, {
name: 'Windows Phones',
children: [{
name: 'Microsoft'
}, {
name: 'Nokia'
}]
}]
}, {
name: 'Cameras',
children: [{
name: 'Digital',
children: [{
name: 'Nikon'
}, {
name: 'Fuji'
}]
}, {
name: 'DSLR',
children: [{
name: 'Canon'
}, {
name: 'Nikon'
}]
}]
}];
You can copy your code is chrome's console to see where the error is.
you need to put double quotes on key an value like this "key" : "value"
[{
"name": "Computers",
"children": [{
"name": "Notebook",
"children": [{
"name": "Apple"
}, {
"name": "Windows"
}]
}, {
"name": "Tablets",
"children": [{
"name": "Apple"
}, {
"name": "Android"
}, {
"name": "Windows"
}]
}]
}]
And after name property values, comma is missing (at various places)
key and value should be wrapped by double quotes
copy paste the code validate here https://jsonformatter.curiousconcept.com/
[
{
"name":"Computers",
"children":[
{
"name":"Notebook",
"children":[
{
"name":"Apple"
},
{
"name":"Windows"
}
]
},
{
"name":"Tablets",
"children":[
{
"name":"Apple"
},
{
"name":"Android"
},
{
"name":"Windows"
}
]
}
]
},
{
"name":"Phones",
"children":[
{
"name":"Android",
"children":[
{
"name":"Samsung"
},
{
"name":"Nokia"
},
{
"name":"Lenovo"
}
]
},
{
"name":"Windows Phones",
"children":[
{
"name":"Microsoft"
},
{
"name":"Nokia"
}
]
}
]
},
{
"name":"Cameras",
"children":[
{
"name":"Digital",
"children":[
{
"name":"Nikon"
},
{
"name":"Fuji"
}
]
},
{
"name":"DSLR",
"children":[
{
"name":"Canon"
},
{
"name":"Nikon"
}
]
}
]
}
]

Categories

Resources