I am getting below json array as and i want to remove the child node "n":[] - when it is empty. (PHP)
INPUT:
[
{
"level": 1,
"id": "101",
"n": [
{
"level": 2,
"id": "102",
"n": [
]
}
]
},
{
"level": 1,
"id": "103",
"n": [
{
"level": 2,
"id": "104",
"n": [
]
},
{
"level": 2,
"id": "105",
"n": [
{
"level": 3,
"id": "106",
"n": [
]
},
{
"level": 3,
"id": "107",
"n": [
{
"level": 4,
"id": "108",
"n": [
{
"level": 5,
"id": "109",
"n": [
]
}
]
}
]
}
]
}
]
},
{
"level": 1,
"id": "110",
"n": [
{
"level": 2,
"id": "111",
"n": [
{
"level": 3,
"id": "112",
"n": [
]
}
]
},
{
"level": 2,
"id": "113",
"n": [
{
"level": 3,
"id": "114",
"n": [
{
"level": 4,
"id": "115",
"n": [
]
}
]
},
{
"level": 3,
"id": "116",
"n": [
{
"level": 4,
"id": "117",
"n": [
]
},
{
"level": 4,
"id": "118",
"n": [
{
"level": 5,
"id": "119",
"n": [
]
},
{
"level": 5,
"id": "120",
"n": [
]
}
]
}
]
}
]
}
]
}
]
OUTPUT:
[
{
"level": 1,
"id": "101",
"n": [
{
"level": 2,
"id": "102"
}
]
},
{
"level": 1,
"id": "103",
"n": [
{
"level": 2,
"id": "104"
},
{
"level": 2,
"id": "105",
"n": [
{
"level": 3,
"id": "106"
},
{
"level": 3,
"id": "107",
"n": [
{
"level": 4,
"id": "108",
"n": [
{
"level": 5,
"id": "109"
}
]
}
]
}
]
}
]
},
{
"level": 1,
"id": "110",
"n": [
{
"level": 2,
"id": "111",
"n": [
{
"level": 3,
"id": "112"
}
]
},
{
"level": 2,
"id": "113",
"n": [
{
"level": 3,
"id": "114",
"n": [
{
"level": 4,
"id": "115"
}
]
},
{
"level": 3,
"id": "116",
"n": [
{
"level": 4,
"id": "117"
},
{
"level": 4,
"id": "118",
"n": [
{
"level": 5,
"id": "119"
},
{
"level": 5,
"id": "120"
}
]
}
]
}
]
}
]
}
]
All the ,"n":[] removed from the json array.
Please help me with some PHP code to get the output from that above input.
Use the recursion, to recursively find the property n which has the value [] and delete them:
function removeEmptyNode(nodes) {
if(!nodes || nodes.length === 0)
return;
nodes.forEach(function(node) {
if(node.n.length === 0) {
delete node.n;
} else {
removeEmptyNode(node.n);
}
});
}
var data = [{
"level": 1,
"id": "101",
"n": [{
"level": 2,
"id": "102",
"n": []
}]
}, {
"level": 1,
"id": "103",
"n": [{
"level": 2,
"id": "104",
"n": []
}, {
"level": 2,
"id": "105",
"n": [{
"level": 3,
"id": "106",
"n": []
}, {
"level": 3,
"id": "107",
"n": [{
"level": 4,
"id": "108",
"n": [{
"level": 5,
"id": "109",
"n": []
}]
}]
}]
}]
}, {
"level": 1,
"id": "110",
"n": [{
"level": 2,
"id": "111",
"n": [{
"level": 3,
"id": "112",
"n": []
}]
}, {
"level": 2,
"id": "113",
"n": [{
"level": 3,
"id": "114",
"n": [{
"level": 4,
"id": "115",
"n": []
}]
}, {
"level": 3,
"id": "116",
"n": [{
"level": 4,
"id": "117",
"n": []
}, {
"level": 4,
"id": "118",
"n": [{
"level": 5,
"id": "119",
"n": []
}, {
"level": 5,
"id": "120",
"n": []
}]
}]
}]
}]
}];
removeEmptyNode(data);
console.log(data);
EDIT:
If you want to do this in PHP, you can convert the above code easily to PHP.
Assuming you have source object in JSON string, you could be able to use this code:
<?php
function removeEmptyNodes(&$nodes) {
if(empty($nodes)) {
return;
}
foreach($nodes as &$node) {
if(empty($node['n'])) {
unset($node['n']);
} else {
removeEmptyNodes($node['n']);
}
}
}
$json = '[{"level":1,"id":"101","n":[{"level":2,"id":"102","n":[]}]},{"level":1,"id":"103","n":[{"level":2,"id":"104","n":[]},{"level":2,"id":"105","n":[{"level":3,"id":"106","n":[]},{"level":3,"id":"107","n":[{"level":4,"id":"108","n":[{"level":5,"id":"109","n":[]}]}]}]}]},{"level":1,"id":"110","n":[{"level":2,"id":"111","n":[{"level":3,"id":"112","n":[]}]},{"level":2,"id":"113","n":[{"level":3,"id":"114","n":[{"level":4,"id":"115","n":[]}]},{"level":3,"id":"116","n":[{"level":4,"id":"117","n":[]},{"level":4,"id":"118","n":[{"level":5,"id":"119","n":[]},{"level":5,"id":"120","n":[]}]}]}]}]}]';
$data = json_decode($json, true);
removeEmptyNodes($data);
print_r(json_encode($data));
?>
Related
I have the following problem. I'd like to convert an array of strings in dot notation to a nested json object.
example input: obj = {"project1.foo.a" : "value", "project1.foo.b" : "value","project1.bar" : "value", "project2.foo.a" : "value", "project2.foo.b" : "value", "project2.foo.c" : "value", "project2.foo.bar" : "value"};
desired output:
{
"id": 0,
"title": "Projects",
"has_children": 1,
"level": 1,
"value":"string",
"children": [
{
"id": 1,
"title": "Project 1",
"has_children": true,
"level": 2,
"value":"string",
"children": [
{
"id": 11,
"title": "foo",
"has_children": true,
"level": 3,
"value":"string",
"children": [
{
"id": 111,
"title": "a",
"has_children": false,
"level": 4,
"value":"string",
"children": [
]
},
{
"id": 112,
"title": "b",
"has_children": false,
"level": 4,
"value":"string",
"children": [
]
}
]
},
{
"id": 12,
"title": "bar",
"has_children": false,
"level": 3,
"value":"string",
"children": [
]
}
]
},
{
"id": 2,
"title": "Project 2",
"has_children": true,
"level": 2,
"value":"string",
"children": [
{
"id": 21,
"title": "foo",
"has_children": true,
"level": 3,
"value":"string",
"children": [
{
"id": 211,
"title": "a",
"has_children": false,
"level": 4,
"value":"string",
"children": [
]
},
{
"id": 212,
"title": "b",
"has_children": false,
"level": 4,
"value":"string",
"children": [
]
},
{
"id": 213,
"title": "c",
"has_children": false,
"level": 4,
"value":"string",
"children": [
]
}
]
},
{
"id": 22,
"title": "bar",
"has_children": false,
"level": 3,
"value":"string",
"children": [
]
}
]
}
]
}
There are great functions to unflatten the example object, e. g. with lodash, but I'm not able to add the descendants recursively into the childrens array.
example code:
const unflatten = (flattedObject) => {
let result = {};
_.keys(flattedObject).forEach(function (key, value){
_.set(result, key, flattedObject[key]);
})
return result;
}
console.log(unflatten(obj));
Maybe someone knows a good/neat or at least working approach to do this. :D I want to feed this plugin with the data https://travistidwell.com/jquery.treeselect.js/.
Related posts: How to populate jquery treeselect widget?
How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?
I am new to JavaScript and Node JS
want to transform the following nested object with student
Data:
[{
"id": 1,
"name": "A",
"children": [{
"id": 2,
"name": "B",
"children": [{
"id": 3,
"name": "C"
},
{
"id": 4,
"name": "D"
}
]
}]
}]
to
Expected:
[{
"student": {
"id": 1,
"name": "A"
},
"children": [{
"student": {
"id": 2,
"name": "B"
},
"children": [{
"student": {
"id": 3,
"name": "C"
}
},
{
"student": {
"id": 4,
"name": "D"
}
}
]
}]
}]
I guess you are seeking a solution for an array with multiple student objects. So you can use the map method to modify them.
const original = [{
"id": 1,
"name": "A",
"children": [{
"id": 2,
"name": "B",
"children": [{
"id": 3,
"name": "C"
},
{
"id": 4,
"name": "D"
}
]
}]
}]
const modified = original.map(stu => {
return {
student: {
id: stu.id,
name: stu.name,
},
children: stu.children
}
})
I have a forest of trees of arbitrary height, more or less like this:
let data = [
{ "id": 2, "name": "AAA", "parent_id": null, "short_name": "A" },
{
"id": 10, "name": "BBB", "parent_id": null, "short_name": "B", "children": [
{
"id": 3, "name": "CCC", "parent_id": 10, "short_name": "C", "children": [
{ "id": 6, "name": "DDD", "parent_id": 3, "short_name": "D" },
{ "id": 5, "name": "EEE", "parent_id": 3, "short_name": "E" }
]
},
{
"id": 4, "name": "FFF", "parent_id": 10, "short_name": "F", "children": [
{ "id": 7, "name": "GGG", "parent_id": 4, "short_name": "G" },
{ "id": 8, "name": "HHH", "parent_id": 4, "short_name": "H" }
]
}]
}
];
And I'm trying to produce a representation of all the root-to-leaves paths, something like this
[
[
{
"id": 2,
"name": "AAA"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 6,
"name": "DDD"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 5,
"name": "EEE"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 4,
"name": "F"
},
{
"id": 7,
"name": "GGG"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 4,
"name": "F"
},
{
"id": 8,
"name": "HHH"
}
]
]
So I wrote the following code:
function flattenTree(node, path = []) {
if (node.children) {
return node.children.map(child => flattenTree(child, [...path, child]));
} else {
let prefix = path.slice(0, path.length - 1).map(n => ({ id: n.id, name: n.short_name }));
let last = path[path.length - 1];
return [...prefix, { id: last.id, name: last.name } ];
}
}
let paths = data.map(n => flattenTree(n, [n]));
but paths comes out with extra nesting, like this:
[
[
{
"id": 2,
"name": "AAA"
}
],
[
[
[
{
"id": 10,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 6,
"name": "DDD"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 5,
"name": "EEE"
}
]
],
[
[
{
"id": 10,
"name": "B"
},
{
"id": 4,
"name": "F"
},
{
"id": 7,
"name": "GGG"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 4,
"name": "F"
},
{
"id": 8,
"name": "HHH"
}
]
]
]
]
I lost count of the many ways in which I tried to fix this, but it does look like the algorithm should not produce the extra nesting -- or my eyes are just so crossed by now that I couldn't see my mistake if someone stuck their finger on it.
Can someone help? Feel free to peruse this JSFiddle https://jsfiddle.net/png7x9bh/66/
The extra nestings are created by map. map just wraps the results into an array and returns them, it doesn't care if it is called on child nodes or not. Use reduce and just concat (or push, whatever suits your performance) the results into the first level array directly:
let data = [{"id":2,"name":"AAA","parent_id":null,"short_name":"A"},{"id":10,"name":"BBB","parent_id":null,"short_name":"B","children":[{"id":3,"name":"CCC","parent_id":10,"short_name":"C","children":[{"id":6,"name":"DDD","parent_id":3,"short_name":"D"},{"id":5,"name":"EEE","parent_id":3,"short_name":"E"}]},{"id":4,"name":"FFF","parent_id":10,"short_name":"F","children":[{"id":7,"name":"GGG","parent_id":4,"short_name":"G"},{"id":8,"name":"HHH","parent_id":4,"short_name":"H"}]}]}];
function flattenTree(node, path = []) {
let pathCopy = Array.from(path);
pathCopy.push({id: node.id, name: node.name});
if(node.children) {
return node.children.reduce((acc, child) => acc.concat(flattenTree(child, pathCopy)), []);
}
return [pathCopy];
}
let result = data.reduce((result, node) => result.concat(flattenTree(node)), []);
console.log(JSON.stringify(result, null, 3));
I am getting this type of json in my $scope of angularjs:
$scope.someStuff = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [
{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
}
I want to use angularjs forEach loop and i want to get only category name,
My expected output:
[{"name":"software"}, {"name":"hardware"}, {"name":"waterwash"}]
You can use Array.map()
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
$scope.someStuff.categoryservices.map((x) => { return { name: x.category.name}})
var obj = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
};
console.log(obj.categoryservices.map((x) => {
return {
name: x.category.name
}
}))
You can use map method by passing a callback function as parameter.
const someStuff = { "id": 2, "service": "bike", "min": "22", "per": "100", "tax": "1", "categoryservices": [ { "id": 32, "category": { "id": 1, "name": "software" } }, { "id": 33, "category": { "id": 2, "name": "hardware" } }, { "id": 34, "category": { "id": 3, "name": "waterwash" } } ] }
let array = someStuff.categoryservices.map(function({category}){
return {'name' : category.name}
});
console.log(array);
root1
child1
child2
grandchild1
grandchild2
child3
root2
child1
child2
grandchild1
greatgrandchild1
I have an object array like tree structure like above, I want to get all unique paths in like this
Food->Dry Food Items->Local Dry Food Items
Food->Dry Food Items->Thai Dry Food Items
Food->Dry Food Items->Others
Food->Fruits
------
------
This is my object
[
{
"id": 1,
"name": "Food",
"parent_id": 0,
"children": [
{
"id": 5,
"name": "Dry Food Items",
"parent_id": 1,
"children": [
{
"id": 11,
"name": "Local Dry Food Items",
"parent_id": 5
},
{
"id": 12,
"name": "Thai Dry Food Items",
"parent_id": 5
},
{
"id": 60,
"name": "Others",
"parent_id": 5
}
]
},
{
"id": 6,
"name": "Fruits",
"parent_id": 1
},
{
"id": 7,
"name": "LG Branded",
"parent_id": 1
},
{
"id": 8,
"name": "Meat",
"parent_id": 1
},
{
"id": 9,
"name": "Sea food",
"parent_id": 1
},
{
"id": 10,
"name": "Vegetables",
"parent_id": 1,
"children": [
{
"id": 14,
"name": "Local Vegetables",
"parent_id": 10
},
{
"id": 15,
"name": "Thai Vegetables",
"parent_id": 10
}
]
},
{
"id": 38,
"name": "Frozen",
"parent_id": 1
},
{
"id": 39,
"name": "IP Kitchen",
"parent_id": 1,
"children": [
{
"id": 40,
"name": "IP Meat",
"parent_id": 39
},
{
"id": 41,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 42,
"name": "IP Ingredients",
"parent_id": 39
},
{
"id": 43,
"name": "IP Sauce",
"parent_id": 39
},
{
"id": 44,
"name": "IP Seafood",
"parent_id": 39
},
{
"id": 45,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 46,
"name": "IP Desert",
"parent_id": 39
}
]
}
]
},
{
"id": 2,
"name": "Beverage",
"parent_id": 0,
"children": [
{
"id": 16,
"name": "Bar",
"parent_id": 2
},
{
"id": 17,
"name": "Coffee & Tea",
"parent_id": 2
},
{
"id": 18,
"name": "In Can",
"parent_id": 2
},
{
"id": 19,
"name": "Water",
"parent_id": 2
},
{
"id": 47,
"name": "IP Bar",
"parent_id": 2
}
]
},
{
"id": 3,
"name": "Disposable",
"parent_id": 0,
"children": [
{
"id": 21,
"name": "Disposable",
"parent_id": 3
}
]
},
{
"id": 4,
"name": "SOE",
"parent_id": 0,
"children": [
{
"id": 20,
"name": "Cleaning Materials",
"parent_id": 4
},
{
"id": 22,
"name": "Chinaware",
"parent_id": 4
}
]
}
];
I get to all the nodes in the tree
function traverse(categories) {
categories.forEach(function (category) {
if (category.children && category.children.length) {
traverse(category.children);
}
else {
}
}, this);
}
You can use recursion and create a function using forEach loop.
var arr = [{"id":1,"name":"Food","parent_id":0,"children":[{"id":5,"name":"Dry Food Items","parent_id":1,"children":[{"id":11,"name":"Local Dry Food Items","parent_id":5},{"id":12,"name":"Thai Dry Food Items","parent_id":5},{"id":60,"name":"Others","parent_id":5}]},{"id":6,"name":"Fruits","parent_id":1},{"id":7,"name":"LG Branded","parent_id":1},{"id":8,"name":"Meat","parent_id":1},{"id":9,"name":"Sea food","parent_id":1},{"id":10,"name":"Vegetables","parent_id":1,"children":[{"id":14,"name":"Local Vegetables","parent_id":10},{"id":15,"name":"Thai Vegetables","parent_id":10}]},{"id":38,"name":"Frozen","parent_id":1},{"id":39,"name":"IP Kitchen","parent_id":1,"children":[{"id":40,"name":"IP Meat","parent_id":39},{"id":41,"name":"IP Starter","parent_id":39},{"id":42,"name":"IP Ingredients","parent_id":39},{"id":43,"name":"IP Sauce","parent_id":39},{"id":44,"name":"IP Seafood","parent_id":39},{"id":45,"name":"IP Starter","parent_id":39},{"id":46,"name":"IP Desert","parent_id":39}]}]},{"id":2,"name":"Beverage","parent_id":0,"children":[{"id":16,"name":"Bar","parent_id":2},{"id":17,"name":"Coffee & Tea","parent_id":2},{"id":18,"name":"In Can","parent_id":2},{"id":19,"name":"Water","parent_id":2},{"id":47,"name":"IP Bar","parent_id":2}]},{"id":3,"name":"Disposable","parent_id":0,"children":[{"id":21,"name":"Disposable","parent_id":3}]},{"id":4,"name":"SOE","parent_id":0,"children":[{"id":20,"name":"Cleaning Materials","parent_id":4},{"id":22,"name":"Chinaware","parent_id":4}]}]
function getNames(data) {
var result = [];
function loop(data, c) {
data.forEach(function (e) {
var name = !c.length ? e.name : c + '->' + e.name;
if (e.children) { loop(e.children, name); }
else {
result.push({ name: name });
}
});
}
loop(data, '');
return result;
}
console.log(getNames(arr))