Tree from array of objects with dot seperated property - javascript

Hello I have an array of objects like this:
data
groups are separated to maximum of 3 levels like: Baseline.name.name.
and i would like to create a tree structure of nested objects. Like this:
[
{
title: "Baseline",
key: "root",
children: [**...NESTED CHILDREN HERE WITH THE SAME STRUCTURE AS ROOT**],
},
];
I need this for a ant design tree component: https://ant.design/components/tree
How do I do this please???
I tried multiple approaches most of them from answers on stack overflow but i didnt manage to get the exact structure i want.

Related

Dynamically remove unused keys from json object

First: I'm aware of other solutions like Removing unused information from JSON but I'm looking for a more dynamic approach.
I have two json objects. The first one looks like this:
{
"key2":"value2",
"key3":{
"key4":"value4",
},
"key6":"value6",
}
And the second json objects looks like this:
{
"key1":"value1",
"key2":"value2",
"key3":{
"key4":"value4",
"key5":"value5"
},
"key6":"value6"
}
Is there a dynamic approach (java or javascript) to ensure that only the matching keys between both json objects are returned? In this case, key1 and key5 should be deleted from the second json because this keys are not present in the first one, but the first array does not always look the same.

Angularjs - How to traverse, add and delete elements in nested arrays

I have a nested array:
$scope.audits = [{
name: '2015',
sections:[{
section:'mm',
id:'1',
subsections: [{
subsection:'aa',
mainelements: ['cc','dd']
},{
subsection:'bb',
mainelements: ['ee','ff','gg']
}]
}]
}];
I want to have a few buttons to add and delete nested elements at will. I cannot use $index because it is multiple layers of array.
Here is my Plunker
This problem got it's crazy solutions out here: AngularJS: traversing nested arrays . I'm not going to rewrite the solutions once again because it's unefficient. Don't forget to use search before asking a question.

Is multi dimensional array what I am looking for?

It will seems basic stuff for most of you guys but I'm stuck on this problem:
So I have 3 categories of fields like this:
-Category 1 = [field1, field2, field3.. etc]
-Category 2 = [field1, field2, field3.. etc]
-Category 3 = [field1, field2, field3.. etc]
And on top of that, I have others elements which has all of those categories but not all fields like this:
Element 1 = Category 1[field3, field2], Category2[Field4], Category3[field1, field5, field2]
How am I supposed to organize those data in javascript (I am using Jquery if it can help)?
Many programming languages support arrays with named indexes.
Arrays with named indexes are called associative arrays (or hashes).
JavaScript does NOT support arrays with named indexes.
In JavaScript, arrays always use numbered indexes.
You may use objects instead:
{
"Element1": {
"Category1": [
"field3",
"field2"
],
"Category2": [
"field4"
],
"Category3": [
"field1",
"field5",
"field2"
]
}
}
You can take a look at MDN is you are a complete beginner to JavaScript and it's data types.
Eloquent Javascript might help too in getting the basics of javascript.
It depends on what you are trying to do with the data but one way could be:
var category1 = ['field1', 'field2', ... ];
var element1 = {
category1: ['field3', 'field2'],
...
}

complicated custom sorting in nested array

I've problem sorting my nested array, says I've json like this
var orders = [{
'orderId': 1,
'sales': [{
'salesNumbers': 3
}]
}, {
'orderId': 2,
'sales': [{
'salesNumbers': 4
}]
}];
and I wish I can sort orderId base on salesNumbers. You may say it's impossible or I made a mistake by putting sales as array but it contain only 1 object which is salesNumbers. That's not a mistake, I just do not want to simplify my problem.
so it's possible to, without changing the data structure, sort orderId base on salesNumbers?? My app demo http://jsfiddle.net/sq2C3/
Since you say the sales array only has one item in it, you can order by salesNumbers like this:
orderBy:'sales[0].salesNumbers'
Here is an update of your fiddle: http://jsfiddle.net/wittwerj/sq2C3/2/

recursive function to fill an array with childs

I'm in a javascript project, and I have to draw a tree using canvas. Like this: http://en.wikipedia.org/wiki/Binary_search_tree#Types
Each node represents a window in my project, and each sub-node is a child of the previous node.
I'm sure that I have to use an array, like:
var node = {
windowId,
childs{}
}
That is, the node would be the root, with its id (windowId) and childs{} is another array that will have the same structure.
For example:
var node = {
windowId,
childs{
windowId //windowId of the child
childs{}
}
}
.
.
.
I'm creating a recursive function, but I haven't almost nothing...could you help me,please?
If each node can become top-level, then all you have to do is figure out a structure that always allows childs. Extending your example, it would be pretty easy to make each list of childs an array.
{
id: 0,
childs: [
{
id: 0.1,
childs: [ ... ]
},
{
id: 0.2,
childs: [ ... ]
},
...
]
}
For the rest, store the current object in a variable, then go through each leaf and add the corresponding indent. If you do it right, you can have a single draw function and call it as many times as you need; you just need to keep track of the level.

Categories

Resources