[
{
"id": "a",
"pid": "a",
"name": "AA",
},
{
"id": "b",
"pid": "a",
"name": "BB",
},
{
"id": "c",
"pid": "a",
"name": "CC",
},
{
"id": "x",
"pid": "b",
"name": "XX",
}
]
Above is the data I got from the database. Every person has an id and a pid, pid points to the person's higher level person's id. If a person has highest level, the id equals pid.
I want to convert the raw data to hierarchical JSON, like this:
[
{
"id": "a",
"name": "AA",
"child": [
{
"id": "b",
"name": "BB"
"child": [
{
"id": "x",
"name": "XX"
}
]
},
{
"id": "c",
"name": "CC"
}
]
}
]
I'm using Node.js.
I suggest you to create a tree and take id === pid as a root for the tree, which works for unsorted data.
How it works:
Basically, for every object in the array, it takes the id for building a new object as parentid for a new object.
For example:
{ "id": 6, "pid": 4 }
It generates this property first with id:
"6": {
"id": 6,
"pid": 4
}
and then with pid:
"4": {
"children": [
{
"id": 6,
"pid": 4
}
]
},
and while all objects are similarly treated, we finally get a tree.
If id === pid, the root node is found. This is the object for the later return.
var data = [
{ "id": "f", "pid": "b", "name": "F" },
{ "id": "e", "pid": "c", "name": "E" },
{ "id": "d", "pid": "c", "name": "D" },
{ "id": "c", "pid": "b", "name": "C" },
{ "id": "a", "pid": "a", "name": "A" },
{ "id": "b", "pid": "a", "name": "B" }
],
tree = function (data) {
var r, o = Object.create(null);
data.forEach(function (a) {
a.children = o[a.id] && o[a.id].children;
o[a.id] = a;
if (a.id === a.pid) {
r = a;
} else {
o[a.pid] = o[a.pid] || {};
o[a.pid].children = o[a.pid].children || [];
o[a.pid].children.push(a);
}
});
return r;
}(data);
console.log(tree);
Influenced by the answer of Nina, this is my resolution just for the record.
function corrugate(data){
var root = "";
return data.reduce((t,o) => {
o.id === o.pid && (root = o.id);
t[o.id] ? t[o.id].name = o.name
: t[o.id] = {id: o.id, name: o.name};
t[o.pid] ? o.pid !== o.id ? t[o.pid].children.push(t[o.id])
: t[o.pid].children = t[o.pid].children || []
: t[o.pid] = {id: o.pid, children: [t[o.id]]};
return t;
},{})[root];
}
var data = [{ "id": "f", "pid": "b", "name": "F" },
{ "id": "e", "pid": "c", "name": "E" },
{ "id": "b", "pid": "a", "name": "B" },
{ "id": "d", "pid": "c", "name": "D" },
{ "id": "c", "pid": "b", "name": "C" },
{ "id": "a", "pid": "a", "name": "A" }
];
console.log(corrugate(data));
Related
Currently i have below Array of Objects
const dataClass = [
{
"id": 101,
"class": [
{
"type": "A",
"value": "A-class"
},
{
"type": "B",
"value": "B-class"
},
{
"type": "C",
"value": "C-class"
}
],
"rank": 1
},
{
"id": 102,
"class": [
{
"type": "D",
"value": "D-class"
},
{
"type": "E",
"value": "E-class"
},
{
"type": "F",
"value": "F-class"
}
],
"rank": 2
},
{
"id": 103,
"class": [
{
"type": "G",
"value": "G-class"
},
{
"type": "H",
"value": "H-class"
},
{
"type": "I",
"value": "I-class"
}
],
"rank": 3
}
];
i need to get dataClass object using all value inside the class object, let say i want to get the second object, so i have to search/input "type": "D", "type": "E", and "type": "F".
return array object/object i expect:
[{
"id": 102,
"class": [
{
"type": "D",
"value": "D-class"
},
{
"type": "E",
"value": "E-class"
},
{
"type": "F",
"value": "F-class"
}
],
"rank": 2
}]
I don't find any solution so far, Thanks for any help.
I added one more object with types D, E, F at rank 4
If you want to return all objects that match your filtration, check result1
and if you just wanna return the first object that matches, check result2
const dataClass = [
{
"id": 101,
"class": [
{
"type": "A",
"value": "A-class"
},
{
"type": "B",
"value": "B-class"
},
{
"type": "C",
"value": "C-class"
}
],
"rank": 1
},
{
"id": 102,
"class": [
{
"type": "D",
"value": "D-class"
},
{
"type": "E",
"value": "E-class"
},
{
"type": "F",
"value": "F-class"
}
],
"rank": 2
},
{
"id": 103,
"class": [
{
"type": "G",
"value": "G-class"
},
{
"type": "H",
"value": "H-class"
},
{
"type": "I",
"value": "I-class"
}
],
"rank": 3
},
{
"id": 104,
"class": [
{
"type": "D",
"value": "D-class"
},
{
"type": "E",
"value": "E-class"
},
{
"type": "F",
"value": "F-class"
}
],
"rank": 4
}
];
const expectedValues = ['D', 'E', 'F'];
//use this if you wanna return all objects that match expectedValues
const result1 = dataClass.filter(el => el.class.every(obj => expectedValues.includes(obj.type)));
console.log('all matched Objects => ', result1);
//use this if you wanna return the first object that match expectedValues
const result2 = dataClass.find(el => el.class.every(obj => expectedValues.includes(obj.type)));
console.log('first matched object => ',result2);
Hope this will help,
const dataClass = [
{
"id": 101,
"class": [
{
"type": "A",
"value": "A-class"
},
{
"type": "B",
"value": "B-class"
},
{
"type": "C",
"value": "C-class"
}
],
"rank": 1
},
{
"id": 102,
"class": [
{
"type": "D",
"value": "D-class"
},
{
"type": "E",
"value": "E-class"
},
{
"type": "F",
"value": "F-class"
}
],
"rank": 2
},
{
"id": 103,
"class": [
{
"type": "G",
"value": "G-class"
},
{
"type": "H",
"value": "H-class"
},
{
"type": "I",
"value": "I-class"
}
],
"rank": 3
}
];
const resultArr = [];
for (const ch_arr of dataClass){
for (const class_arr of ch_arr["class"]){
if(["D","E","F"].includes(class_arr["type"])){
resultArr.push(ch_arr);
break;
}
};
};
// resultArr is the expected array
You need find the object inside of class Array so i think using find method is the more readable way to solved it
function findClassByType(value: string) {
return [dataClass.find((obj) => obj.class.find(({ type }) => type.toLocaleLowerCase() === value.toLocaleLowerCase()))];
}
console.log(findClassByType('a'))
I added the toLocaleLowerCase to avoid case sensitive.
I have an object that I would like to filter and only return the objects where salesPersonID = "1"
var jsonData = {
"a": {
"id": "a",
"name": "Lucifer Enterprises",
"salesPersonId": "1"
},
"b": {
"id": "b",
"name": "Charlies Chocolate Factory",
"salesPersonId": "1"
},
"c": {
"id": "c",
"name": "Geckos Investments",
"salesPersonId": "2"
}
};
Expected output:
var jsonDataFiltered = {
"a": {
"id": "a",
"name": "Lucifer Enterprises",
"salesPersonId": "1"
},
"b": {
"id": "b",
"name": "Charlies Chocolate Factory",
"salesPersonId": "1"
}
};
What I have tried
Using filter directly on the object which results in Uncaught TypeError: jsonData.filter is not a function
var filteredJsonData = jsonData.filter(function (row){
console.log("test");
});
Using Object entries and filter which returns a and b but with a different structure that will be an issue for what I am using the object for down the line.
var filteredJsonData = Object.entries(jsonData).filter(function (entry){
return entry[1].salesPersonId == "1";
});
Output from test 2 which has the right values but the wrong structure:
[
[ "a", { "id": "a", "name": "Lucifer Enterprises", "salesPersonId": "1" } ],
[ "b", { "id": "b", "name": "Charlies Chocolate Factory", "salesPersonId": "1" } ]
]
The question
How can I get the desired output?
You can use Object.fromEntries:
var jsonData = {
"a": {
"id": "a",
"name": "Lucifer Enterprises",
"salesPersonId": "1"
},
"b": {
"id": "b",
"name": "Charlies Chocolate Factory",
"salesPersonId": "1"
},
"c": {
"id": "c",
"name": "Geckos Investments",
"salesPersonId": "2"
}
};
var filteredJsonData = Object.fromEntries(Object.entries(jsonData).filter(function (entry){
return entry[1].salesPersonId == "1";
}));
console.log(filteredJsonData);
Was wondering if anyone knows of a way to use lodash, or vanilla JS to achieve this small problem?
I have this starting object:
{
"1": {
"null": {
"2": {
"3": {
"6": {
"7": "c"
},
"null": {
"null": {
"5": "b"
}
}
}
}
}
},
"8": {
"10": "e",
"null": {
"9": "d"
}
}
}
Each level (horizontally) means something. So level 1 is of type A, level 2 is of type B, 3 of type A, 4 of type B and so forth. So it alternates.
Is there a nice and simply way to "collapse" this object to look something like this:
[
{
"type": "A",
"label": "1",
"children": [
{
"type": "A",
"label": "2",
"children": [
{
"type": "B",
"label": "3",
"children": [
{
"type": "A",
"label": "6",
"children": [
{
"type": "A",
"label": "7",
"value": "c"
}
]
},
{
"type": "A",
"label": "8",
"children": [
{
"type": "A",
"label": "5",
"value": "b"
}
]
}
]
}
]
}
]
},
{
"type": "A",
"label": "8",
"children": [
{
"type": "B",
"label": "10",
"value": "e"
},
{
"type": "A",
"label": "9",
"value": "d"
}
]
}
]
In essence annotating each level with what type it is, and nesting its children.
Here is the code
function transformObj(obj, level) {
level = level || 1;
var result = _(obj).transform(function(result, value, key) {
var obj = {
type: (level % 2 === 0) ? 'B' : 'A',
label: key
};
if (key === 'null') {
result.push(transformObj(value, level+1));
} else {
if (_.isObject(value)) {
obj.children = transformObj(value, level+1);
} else {
obj.value = value;
}
result.push(obj);
}
}, [])
.flatten()
.value();
return result;
}
Here is the output
[
{
"type": "A",
"label": "1",
"children": [
{
"type": "A",
"label": "2",
"children": [
{
"type": "B",
"label": "3",
"children": [
{
"type": "A",
"label": "6",
"children": [
{
"type": "B",
"label": "7",
"value": "c"
}
]
},
{
"type": "A",
"label": "5",
"value": "b"
}
]
}
]
}
]
},
{
"type": "A",
"label": "8",
"children": [
{
"type": "B",
"label": "10",
"value": "e"
},
{
"type": "A",
"label": "9",
"value": "d"
}
]
}
]
This should do the trick:
var source = {
"1": {
"null": {
"2": {
"3": {
"6": {
"7": "c"
},
"null": {
"null": {
"5": "b"
}
}
}
}
}
},
"8": {
"10": "e",
"null": {
"9": "d"
}
}
};
function collapse(obj, parent, level){
var result = parent || [];
level = level || 0;
for(prop in obj){
var item = obj[prop];
var build = {
type : level % 2 ? "B" : "A",
label : prop
//, level : level
}
if(typeof item == 'object'){
build.children = [];
collapse(item, build.children, level + 1);
} else {
build.value = item;
}
result.push(build);
}
return result;
}
var output = collapse(source);
var result = JSON.stringify(output, null, ' ');
console.log(result);
var elem = document.getElementById("result");
elem.innerHTML = result;
<pre id="result"></pre>
function doIt(data){
return _.chain(data)
.transform(function(result, value, key){
if(key !== 'null'){
var type = _.parseInt(key) % 2 === 1 ? 'A' : 'B';
if(_.isObject(value) && !_.includes(_.keys(value), 'prop1')){
result.push({
type: type,
label: key,
children: doIt(value)
});
} else {
result.push({
type: type,
label: key,
value: value
});
}
} else {
if(_.isObject(value)){
result.push(doIt(value));
}
}
}, [])
.flatten()
.value();
}
var result = doIt(data);
result is
[
{
"type": "A",
"label": "1",
"children": [
{
"type": "B",
"label": "2",
"children": [
{
"type": "A",
"label": "3",
"children": [
{
"type": "B",
"label": "6",
"children": [
{
"type": "A",
"label": "7",
"value": "c"
}
]
},
{
"type": "A",
"label": "5",
"value": "b"
}
]
}
]
}
]
},
{
"type": "B",
"label": "8",
"children": [
{
"type": "B",
"label": "10",
"value": "e"
},
{
"type": "A",
"label": "9",
"value": "d"
}
]
}
]
I have two variables array1 and array2 as following and I want to put the values of array2 into array1 for the properties present in array1 and rest of the properties should remain the same with default values.
One solution I have is to iterate through the array length and set values for found properties and but my array is too long to perform iteration (the array supplied in this question is just a raw value).
I need some better way other than iteration.
var array1=[
{
"name": "a",
"value": 0,
"level": [
{
"name": "a1",
"value": 0
},
{
"name": "a2",
"value": 0
}
]
},
{
"name": "b",
"value": 0,
"level": [
{
"name": "b1",
"value": 0
},
{
"name": "b2",
"value": 0
}
]
},
{
"name": "c",
"value": 0,
"level": [
{
"name": "c1",
"value": 0
},
{
"name": "c2",
"value": 0
}
]
}
]
var array2=[
{
"name": "a",
"value": 1,
"level": [
{
"name": "a1",
"value": 1
},
{
"name": "a2",
"value": 0
}
]
},
{
"name": "b",
"value": 1,
"level": [
{
"name": "b1",
"value": 0
},
{
"name": "b2",
"value": 1
}
]
}
]
and the required output is
var final_array=[
{
"name": "a",
"value": 1,
"level": [
{
"name": "a1",
"value": 1
},
{
"name": "a2",
"value": 0
}
]
},
{
"name": "b",
"value": 1,
"level": [
{
"name": "b1",
"value": 0
},
{
"name": "b2",
"value": 1
}
]
},
{
"name": "c",
"value": 0,
"level": [
{
"name": "c1",
"value": 0
},
{
"name": "c2",
"value": 0
}
]
}
]
A recursive solution with two iterators, one for arrays iterateA and one for objects iterateO. This proposal uses the thisArg for reference to json2.
Basically both callbacks iterates over the items or keys and checks if this is set and if the actual item is present in this. if not, the function returns.
The rest is straight forward, if an object is found, then it iterates over the keys and the object, if an array is found, the it it iterates over the array.
Only in the case of k === 'value' the value of this[k] is to o[k] assigned.
var json1 = [{ "name": "a", "value": 0, "level": [{ "name": "a1", "value": 0 }, { "name": "a2", "value": 0 }] }, { "name": "b", "value": 0, "level": [{ "name": "b1", "value": 0 }, { "name": "b2", "value": 0 }] }, { "name": "c", "value": 0, "level": [{ "name": "c1", "value": 0 }, { "name": "c2", "value": 0 }] }],
json2 = [{ "name": "a", "value": 1, "level": [{ "name": "a1", "value": 1 }, { "name": "a2", "value": 0 }] }, { "name": "b", "value": 1, "level": [{ "name": "b1", "value": 0 }, { "name": "b2", "value": 1 }] }];
function iterateO(o) {
return function (k) {
if (!this || !(k in this)) {
return;
}
if (typeof o[k] === 'object') {
Object.keys(o[k]).forEach(iterateO(o[k]), this[k]);
return;
}
if (Array.isArray(o[k])) {
o[k].forEach(iterateA, this[k]);
return;
}
if (k === 'value') {
o[k] = this[k];
}
};
}
function iterateA(a, i, aa) {
if (!this || !(i in this)) {
return;
}
if (typeof a === 'object') {
Object.keys(a).forEach(iterateO(a), this[i]);
return;
}
if (Array.isArray(a)) {
a.forEach(iterateA, this[i]);
return;
}
}
json1.forEach(iterateA, json2);
document.write('<pre>' + JSON.stringify(json1, 0, 4) + '</pre>');
This is my data which is assigned to $scope.tree:
var data = [
{
"name": "vashi",
id: "vashi",
"children": [{
"name": "a",
id: "S1vashi",
"children": [{
"name": "bayQ",
"id": "S1T1vashi",
"children": ["a", "b", "c"]
}, {
"name": "bayQ1",
"id": "S1T2vashi",
"children": [{
"name": "a"
}]
}]
}, {
"name": "b",
id: "S1vashi",
"children": [{
"name": "bayQ",
"id": "S1T1vashi",
"children": ["a", "b", "c"]
}, {
"name": "bayQ1",
"id": "S1T2vashi",
"children": ["a", "b", "c"]
}]
}]
},
{
"name": "andheri",
id: "vashi",
"children": [{
"name": "a",
id: "S1vashi",
"children": [{
"name": "bayQ",
"id": "S1T1vashi",
"children": ["a", "b", "c"]
}, {
"name": "bayQ1",
"id": "S1T2vashi",
"children": ["a", "b", "c"]
}]
}, {
"name": "b",
id: "S1vashi",
"children": [{
"name": "bayQ",
"id": "S1T1vashi",
"children": ["a", "b", "c"]
}, {
"name": "bayQ1",
"id": "S1T2vashi",
"children": ["a", "b", "c"]
}]
}]
},
]
HTML Part:
<div data-angular-treeview="false" data-tree-model="tree" data-node-id="id" data-node-label="name" data-node-children="children" data-tree-id="tree" data-ng-click="selectNode(currentNode.name)">
Click Method
$scope.selectNode = function(val) {console.log(val); }
I want to select multiple children in different cities. When I select the children it's consoling undefined.