Php array into javascript - javascript

Have found examples, but nothing that works with what I'm trying to.
Have a very simple array of 'text'. I want to replace the text in the below javascript using a for each statement. Could someone point me in the right direction to replace the "text: 'Design'" items with my array?
$text= array("Display", "Training", "Separate");
var $treeData = [
{
text: 'Design',
tags: ['40%'],
href: '#parent3'
},
{
text: 'Coding',
tags: ['60%'],
href: '#parent4'
},
{
text: 'Marketing',
href: '#parent5'
}
];

Related

Move properties in objects according to their index

I've been trying to do this, and I've looked for the answer everywhere but no one seems to know or have the same problem.
What I want to do is: I have an array that looks like this
const myArray = [
{
nombre: 'String',
definicion: 'Whatever.',
duracion: 'Blah',
id: "Blah",
},
{
nombre: 'Name',
definicion: 'Short description.',
duracion: 'time',
id: "whatever again",
},
{
nombre: 'hello',
definicion: 'this is descripciĆ³n.',
duracion: 'time',
id: "the end",
},
I used map return (item. nombre, item. definicion, etc.) and have them separated in different divs, so I wanted to do nombre.onclick (CSS transition so that the its definition will be shown) but its not working. It either shows all definitions or none. Sometimes it shows one definition 3 times ( or the number of objects in the array)

Serialize a strange js object

I got a js object, if I use the console.log, the result is like this:
{ title: 'testCase',
id: '5e41538bbf4a7e93a12925a3e6ca12',
links:
[ URLLink {
link: 'www.google.com' } ]
}
but If I use console.log("sometext" + wireObj);
The result is :
sometext[object Object]
If I use JSON.stringify(), the result will become:
{ title: 'testCase',
id: '5e41538bbf4a7e93a12925a3e6ca12',
links:
[{
link: 'www.google.com' }]
}
As you can see, the URLLink is missing, I would like to preserve this information as well. Any comments?
If what you're trying to do is model links as an array of URLLink objects then maybe you could do it like this:
{ title: 'testCase',
id: '5e41538bbf4a7e93a12925a3e6ca12',
links:
[ {URLLink: {link: 'www.google.com' }},
{URLLink: {link: 'www.yahoo.com' }} ]
}
Note the curly braces wrapping URLLink and the colon mentioned by #alexandru-ionut-mihai
That would be parsed like below:

Efficient algorithm / recursive function to nest items from a list

I'm currently implementing my own commenting system. Unfortunately Disqus or any other comment platform doesn't meet my requirements.
I use NodeJS and MongoDB as backend. I need to run basically two queries on my database:
Get all comments by a topic/slug
Get all comments by a user
One can comment to an topic or reply to a comment.
Hey, cool post # top lvl comment
Thanks! # reply to comment
Foo Bar! # reply to reply
and so on...
So my database schema looks like
{
id: ObjectId,
text: string,
author: { id: ObjectId, name: string },
parent: nullable ObjectId,
slug: string/number/whatever
}
If parent is null it's a top level comment, otherwise it's a reply.
Pretty easy so far, right? The problem I do have now is displaying comments below posts. When there would be only top level comments it would be easy. Just get all comments for one specific slug, sort them by date/rating/... and compile them with my HTML View Engine.
But there are in fact replies and I'm just stuck at the point where I need to organize my structure. I want to nest replies into comments within my list
Original list (simplified)
[
{ id: 1, text: 'foo', parent: null },
{ id: 2, text: 'bar', parent: 1 },
// ...
]
Expected Output
[
{ id: 1, text: 'foo', replies: [
{ id: 2, text: 'bar' },
] },
]
I've tried creating my expected output with a recursive function which got very weird tho. Unless that it wouldn't be very efficient. So since I'm really getting frustrated and kinda feeling stupid not solving this problem I've decided to ask for your help SO.
The actual problem I want to solve: How do I render my comments, that they are properly nested etc.
The question I'm going to ask: How do I organize my flat structure in an efficient way to solve the above described problem?
Here's one approach with linear complexity:
var comments = [{
id: 3,
text: 'second',
parent: 1
}, {
id: 1,
text: 'root',
parent: null
}, {
id: 2,
text: 'first',
parent: 1
}, {
id: 5,
text: 'another first',
parent: 4
}, {
id: 4,
text: 'another root',
parent: null
}];
var nodes = {};
//insert artificial root node
nodes[-1] = {
text: 'Fake root',
replies: []
};
//index nodes by their id
comments.forEach(function(item) {
if (item.parent == null) {
item.parent = -1;
}
nodes[item.id] = item;
item.replies = [];
});
//put items into parent replies
comments.forEach(function(item) {
var parent = nodes[item.parent];
parent.replies.push(item);
});
//root node replies are top level comments
console.log(nodes[-1].replies);

Convert JSON into Array of JavaScript Objects

I got a JSON result from PHP that looks like this below. I need to convert it into an array of objects like shown at the bottom.
How can I achieve this?
What I have
Milestones JSON
[
{
"id":0,
"name":"None"
},
{
"id":1,
"name":"Milestone 1"
},
{
"id":2,
"name":"Milestone 2"
},
{
"id":3,
"name":"Milestone 3"
},
{
"id":4,
"name":"Milestone 4"
}
]
What I need
Milestones Array Of OBjects
var taskMilestonesArray = [{
id: 0,
name: 'None',
},
{
id: 1,
name: 'Milestone 1',
},
{
id: 2,
name: 'Milestone 2',
},
{
id: 3,
name: 'Milestone 3',
},
{
id: 4,
name: 'Milestone 4',
}];
UPDATE
I just realized they are both in almost exact same format already. I just need to pass the array of objects into a library that expects it to be in that format and I don't think I can pass the JSON in.
If you have that JSON in a string (for the sake of the example, I will assume you have a variable named yourJsonString that holds your json), you can parse it:
var taskMilestonesArray = JSON.parse(yourJsonString);
Use JSON.parse api to convert json string to the javascript object.
var taskMilestonesArray = JSON.parse('< milestones json string >');

Converting JSON to DynaTree Object

I have following data Structure which I want to use in data structure given in Dyntree Format:
[
{
title: "my Title",
isFolder: "true",
key: 1
}
]
I am setting above data structure as STRING in a JS variable.
When I set this value as a javascript variable, it gives error of Invalid Type
DynTree format is given below;
var fakeJsonResult = [
{ title: 'Lazy node 1', isLazy: true },
{ title: 'Simple node 2', select: true }
];
This is simple if you want to create a dynatree from the object you mentioned simply do this
var children = [
{
title: "my Title",
isFolder: "true",
key: 1
}
];
$('#tree').dynatree({
children : children
});

Categories

Resources