You can use the Spread Operator to accomplish that.
This code snippet has a function called addElements which find the target and adds the new elements to the children array.
var obj = {
"name": "Root",
"children": [{
"name": "child1"
},
{
"name": "child2"
}
]
};
var newArray = [
{ "name": "child11"},
{ "name": "child12"}
];
var addElements = function(target, array) {
obj.children.forEach(function(child) {
if (child.name === target) {
child['children'] = [...(child['children'] || []), ...newArray];
return;
}
});
};
addElements('child1', newArray);
console.log(obj);
See? now your obj.childre[0].children array contains the new elements.
I would do below if I imagine well your typo:
const foo = { a: 'vala', b: 'valb', childval : { x : 'valx' } };
const bar = { c: 'valc' };
foo.childval = { ...foo.childval, ...bar };
spread syntax is neat for these situations
Try this
let obj1 = {a : 'vala' , b:'valb' , childval : { x : 'valx'} }
let obj2 = {c: 'valc'};
Object.assign(obj1.childval, obj2);
console.log(obj1);
reactjs - Adding data to a nested JSON object with children based on an array - Javascript/REACT - Stack Overflow
node.js - NodeJS: Adding new child nodes to JSON Object - Stack Overflow
json - How to add an object to a nested javascript object using a parent id - Stack Overflow
Add new attribute (element) to JSON object using JavaScript - Stack Overflow
See this fiddle: http://jsfiddle.net/2Dvws/
It will find an object by ID. And push the new child. Since every object within Javascript is a reference, you can return it as an var.
var ob = {
name: "root",
id: 1,
children: [
{
name: "child one",
id: 11,
children: [
{
name: "grand child 1",
id: 111,
children: []},
{
name: "grand child 2",
id: 112,
children: []}
]},
{
name: "child two",
id: 12,
children: []}
]
};
The function which will return the found element. Will look into all child elements.
function findObjectById(root, id) {
if (root.children) {
for (var k in root.children) {
if (root.children[k].id == id) {
return root.children[k];
}
else if (root.children.length) {
return findObjectById(root.children[k], id);
}
}
}
};
var bla = findObjectById(ob, 111);
console.log(bla);
bla.children.push({
name: "child x",
id: 1111,
children: []
});
console.log(ob);
Output is that child with id 111 will have 1 child with id 1111
I assume the id consists of the parent-id plus the index (1 to 9) in the children array? Then you can go like that:
var rootobj = {…};
var newnode = {name: "grandchild three", id: 113, children:[]};
var id = ""+newnode.id;
var cur = [rootobj];
for (var i=0; i<id.length-i; i++)
cur = cur[id.charAt(i)-1].children;
cur[id.charAt(i)-1] = newnode;
JSON stands for JavaScript Object Notation. A JSON object is really a string that has yet to be turned into the object it represents.
To add a property to an existing object in JS you could do the following.
object["property"] = value;
or
object.property = value;
If you provide some extra info like exactly what you need to do in context you might get a more tailored answer.
var jsonObj = {
members:
{
host: "hostName",
viewers:
{
user1: "value1",
user2: "value2",
user3: "value3"
}
}
}
for(var i=4; i<=8; i++){
var newUser = "user" + i;
var newValue = "value" + i;
jsonObj.members.viewers[newUser] = newValue ;
}
console.log(jsonObj);
What i basically understood about what you wanna to do is this:
var obj = {
children:[]
}
obj.children.push({test:'Hello world'});
console.log(obj.children);//[Object { test="Hello world"}]
Am i right?
Your initial tree:
[
{"name":"Item 1 Name","children":[]},
{"name":"Item 2 Name","children":[]}
]
Your second REST call
{"name":"Item 3 Name","id":"3","parentid":"2","children":[]},
{"name":"Item 4 Name","id":"4","parentid":"2","children":[]}
and your code:
addBranch: function(branch, tree){
for (var i=0; i<branch.length; i++){
network = _.filter(tree, function(obj){
if(obj.id == branch[i].parentId){
obj.children.push(branch[i]);
}
});
}
return tree;
},
two things that i noticed, your initial tree doesn't have a "id" or "parent id", how this initial tree would work with your algorithm above?
your algorithm is "simple", just check if the object(object1) has a "parent id" attribute, if it have, you just hold this object, find in your array of objects(your tree) if an object has (id === parent_id), if true, you just push your object(object1) inside this another object array atribute "children" and after that you just remove your object(object1) from original resource, and look for another object to push in your tree.
You have nested arrays, so you need to access the elements in the arrays by their index.
Copyvar hold = body.rows[0].elements[0].duration_in_traffic.text;
you need to parse the JSON string (if it is a string) first, then you can access it ... assuming body is the var that holds the string, you would do
Copyvar hold = JSON.parse(body).rows[0].elements[0].duration.text;
or
Copyvar obj = JSON.parse(body);
var hold = obj.rows[0].elements[0].duration.text;
Hi all,
We have a comment JSON file that looks like this:
{
"comment": "This is a comment.",
"parent_id": 0,
"name": "Kelvin",
"date": 2014-06-09,
"id": 118751,
},
{
"comment": "This is a reply.",
"parent_id": 118751,
"name": "Natalie",
"date": 2014-05-22,
"id": 111247,
},
{
"comment": "This is another reply.",
"parent_id": 118751,
"name": "John",
"date": 2014-05-22,
"id": 111347,
}
{
"comment": "This is another parent comment.",
"parent_id": 0,
"name": "Steven",
"date": 2014-06-09,
"id": 118752,
},
{
"comment": "This is a reply to Steven's comment.",
"parent_id": 118752,
"name": "Natalie",
"date": 2014-05-22,
"id": 111247,
},
{
"comment": "This is another reply to Steven's comment.",
"parent_id": 118752,
"name": "John",
"date": 2014-05-22,
"id": 111347,
}We map over this to list out the comments on our blog, desc by date.
Here's the issue:
We need to restructure this so that child comments are somehow under their parents:
parent:
{
"comment": "This is a comment.",
"parent_id": 0,
"name": "Kelvin",
"date": 2014-06-09,
"id": 118751,
},
children: [
{
"comment": "This is a reply.",
"parent_id": 118751,
"name": "Natalie",
"date": 2014-05-22,
"id": 111247,
},
{
"comment": "This is another reply.",
"parent_id": 118751,
"name": "John",
"date": 2014-05-22,
"id": 111347,
}
]
parent:
{
"comment": "This is another parent comment.",
"parent_id": 0,
"name": "Steven",
"date": 2014-06-09,
"id": 118752,
},
children: [
{
"comment": "This is a reply to Steven's comment.",
"parent_id": 118752,
"name": "Natalie",
"date": 2014-05-22,
"id": 111247,
},
{
"comment": "This is another reply to Steven's comment.",
"parent_id": 118752,
"name": "John",
"date": 2014-05-22,
"id": 111347,
}
]And so on.
So basically we'd map over it to get the parent comments, and then map over it again to get the children of those parent comments, and then somehow combine it all together.
Is there an easy way to do this? Stumped. (this is a React/NextJS site)
You can see that the first/parent comment has parent_id of 0, and then child comments have parent_id that matches the id of the parent.
Much appreciated.
try this
$.each(resp.message, function(index, dogs) {
var subdog = dogs.length;
console.log(subdog);
});
You can use Object.values() to get an array of the values of the object and .map() to return .length of each array that is a value of a property within the object
let o = {"status":"success","message":{"affenpinscher":[],"african":[],"airedale":[],"corgi":["cardigan"],"akita":[],"appenzeller":[],"basenji":[],"beagle":[],"bluetick":[],"borzoi":[],"bouvier":[],"boxer":[],"brabancon":[],"briard":[],"bulldog":["boston","french"]}};
let len = Object.values(o.message).map(({length}) => length);
console.log(len);
In your JSON tests is not an array but an object, and you cannot push to an object. So this line is wrong:
ctrl.otsact.tests.push({});
You have to change it to:
ctrl.otsact.tests[MYNEWID] = MYNEWOBJECT;
Your tests property is...an object, not an array. Tests should be tests: [], not tests: {}.