Nested merging of JSON Objects in Javascript / NodeJS - sql

I have two SQL Queries that return their data as JSON using NodeJS Seriate.
First is categories with two fields, category and name.
categories = [{ category: '1', name: 'category1' },
{ category: '2', name: 'category2' }]
Second one is branches with three fields, category, branch and branchName.
branches = [{ category: '1', branch: '1', name: 'branch1' },
{ category: '1', branch: '2', name: 'branch2' },
{ category: '2', branch: '3', name: 'branch3' },
{ category: '2', branch: '4', name: 'branch4' }]
I want to create one merged JSON, based on the category of each JSON that would have the following structure:
all = [
{ category: '1', name: 'category1',
branches: [
{branch: '1', name: 'branch1' },
{branch: '2', name: 'branch2' }],
{ category: '2', name: 'category2',
branches: [
{branch: '3', name: 'branch3'},
{branch: '4', name: 'branch4'}]
]
I searched and found several methods of joining or extending two json objects but nothing similar to that.
Any help would be appreciated.

This should do it:
categories.forEach(function(c) {
c.branches = branches.filter(function(b) {
var tmp = b.category;
delete b.category;
return tmp === c.category;
});
})

Related

How to generate json from two SQL queries?

Tell me who knows how to make a json response of the desired format from two sql requests.
Base (client groups) #1 clients_groups has id_clients and name columns.
id_clients here are client IDs through whom from the clients database.
name is the name of the section.
Base (List of clients) No. 2 clients has data about users.
How to make json. Example as needed:
`
{
id: '',
text: 'Department №1',
children: [ // Here at the bottom you need to get a list of users by ID from department No. 1
{ id: 'User ID', text: 'Username' },
{ id: 'User ID', text: 'Username' }
]
},
{
id: '',
text: 'Department №2',
children: [ // Here at the bottom you need to get a list of users by ID from department No. 2
{ id: 'User ID', text: 'Username' },
{ id: 'User ID', text: 'Username' }
]
}
`
I tried to link data from an attachment through a loop. Nothing helped.
query("SELECT id, id_clients, name FROM clients_groups", (error, results) => {
console.log(results);
})
[
RowDataPacket { id: 1, id_clients: '2', name: 'Manager' },
RowDataPacket { id: 2, id_clients: '2, 3', name: 'Support' }
]

How do I perform a SQL update on a list inside one of my table rows?

I have a table with parameters as shown below.
{
id: 1,
title: 'waccos',
description: 'Making an operating system in wacc',
est_duration: 420,
exp_req: 'any',
langs: [ 1 ],
owner: 1,
members: [ 1, 2 ]
},
I want to use SQL to add a new member into the member list, but I do not know how to. The table is called testprojects. I've tried using
(update testprojects
set members = members || $1
where title is $2;', [name, title])
but I think that is wrong. Could anyone help me please?
This is the table of members so far.
[
{ id: 1, name: 'A' },
{ id: 2, name: 'K' },
{ id: 3, name: 'S' },
{ id: 5, name: 'd' },
{ id: 6, name: 'J' },
{ id: 7, name: 'E' },
{ id: 8, name: 'a' }
]

How to push array to state several times in react native SetState?

I would like to push several arrays to the same state(array) like below.
constructor(props){
super(props);
this.state = {
itemsArray1: [],
itemsArrayMixed: [],
}
}
handleSetState1() {
this.setState({
itemsArray1: [
{name: 'aaa', id: '001', address: 'xxx' },
{name: 'aaa', id: '002', address: 'yyy'},
{name: 'ccc', id: '003', address: 'zzz'},
]
});
this.setState({ itemsArrayMixed: [...itemsArray1] });
}
handleSetState2() {
this.setState({
itemsArray1: [
{name: 'ddd', id: '004', address: 'ttt' },
{name: 'eee', id: '005', address: 'uuu'},
{name: 'fff', id: '006', address: 'www'},
]
});
this.setState({ itemsArrayMixed: [...itemsArray1] });
}
console.log(itemsArrayMixed) // => in itemsArrayMixed, I want to get 6 objects.
Above code is just an example.
In my project, I import 6 arrays that include objects from a different store.
And I want to push all these arrays to the same state (in this case, itemsArrayMixed).
Of course, if I want to handle only one array, that's easy to handle and I know how to do that.
However, I don't know how to push multi arrays to the same state...
You are just adding the new array(itemsArray1) in the mixed array without getting its previous value. you should try this and it will work
handleSetState1() {
let itemArray1 = [
{name: 'aaa', id: '001', address: 'xxx' },
{name: 'aaa', id: '002', address: 'yyy'},
{name: 'ccc', id: '003', address: 'zzz'},
]
this.setState({
itemsArray1
});
this.setState({ itemsArrayMixed: [...this.state.itemsArrayMixed, itemArray1] });
}
This will update the itemArrayMixed and store all value.

Dojo Gridx with nested data store?

EDIT: the answer is to use a formatter function (see below)
If you have a data store something like this:
var store = new Store({
data: [
{ id: 1, username: {first: 'John', last: 'Doe'},
score: 130, city: 'New York', birthday: '1980/2/5'}
]
});
Is there a way to tell GridX to reference username.first in a structure something like this? (this doesn't work, but is there a way to do this?)
var columns = [
{field: 'username.first', name: 'Name'}
];
ANSWER:
var columns = [
{name: 'Name', field: 'username',
formatter: function(rowData) {
return rowData.username.first;
}
}
];

Sorting JSON and displaying in Tree using DOJO

I am learning DOJO 1.6.
I have data
var data = [
{ FirstName: 'xyz', Lastname: 'QSD', rollNo: '1', EntryDate: '2012-09-11T17:35:31.835+02:00' },
{ FirstName: 'abc', Lastname: 'qgr', rollNo: '2', EntryDate: '2012-08-11T17:35:31.835+02:00' }
{ FirstName: 'ert', Lastname: 'fgd', rollNo: '3', EntryDate: '2012-18-11T17:35:31.835+02:00' }
];
I want to sort it with respect to Last name or EntryDate and display in a tree format.
Thanks in Advance.
Multiple root data
data: [
{ id: 'world', name:'The earth', type:'planet', population: '6 billion'},
{ id: 'AF', name:'Africa', type:'continent', population:'900 million', area: '30,221,532 sq km',
timezone: '-1 UTC to +4 UTC', parent: 'world'},
{ id: 'EG', name:'Egypt', type:'country', parent: 'AF' },
{ id: 'KE', name:'Kenya', type:'country', parent: 'AF' },
{ id: 'Nairobi', name:'Nairobi', type:'city', parent: 'KE' },
{ id: 'Mombasa', name:'Mombasa', type:'city', parent: 'KE' },
{ id: 'SD', name:'Sudan', type:'country', parent: 'AF' },
{ id: 'Khartoum', name:'Khartoum', type:'city', parent: 'SD' },
{ id: 'AS', name:'Asia', type:'continent', parent: 'world' },
{ id: 'CN', name:'China', type:'country', parent: 'AS' },
{ id: 'IN', name:'India', type:'country', parent: 'AS' },
{ id: 'RU', name:'Russia', type:'country', parent: 'AS' },
{ id: 'MN', name:'Mongolia', type:'country', parent: 'AS' },
{ id: 'OC', name:'Oceania', type:'continent', population:'21 million', parent: 'world'},
{ id: 'EU', name:'Europe', type:'continent', parent: 'world' },
{ id: 'DE', name:'Germany', type:'country', parent: 'EU' },
{ id: 'FR', name:'France', type:'country', parent: 'EU' },
{ id: 'ES', name:'Spain', type:'country', parent: 'EU' },
{ id: 'IT', name:'Italy', type:'country', parent: 'EU' },
{ id: 'NA', name:'North America', type:'continent', parent: 'world' },
{ id: 'SA', name:'South America', type:'continent', parent: 'world' }
],
Javascript Array has a native function, called sort. This will off-the-shelf sort the values alphabetically. For the purpose of sorting non-string-values, we need to supply a sortingfunction. Like so, in regards to Lastname:
data.sort(function(a,b) {
var _A=a.Lastname.toLowerCase(),
_B=b.Lastname.toLowerCase();
if (_A < _B) //sort string ascending
return -1
if (_A > _B)
return 1
return 0 //default return value (no sorting)
});
If youre sorting against a date, you would need to initialize _A and _B to a Date.
However, if youre aiming to represent the data in a dijit.Tree, there's inbuilt method for sorting the Store, lets wrap data into a dojo/data/ItemFileReadStore and show in a tree. Tree will have a model, using ItemFileWriteStore - so that items can be modified:
var sortableStore = new dojo.data.ItemFileReadStore({
data: {
identifier: 'rollNo',
items: data
},
comperatorMap: {
'EntryDate' : function(a,b) {
var _A = new Date(a), _B = new Date(b);
if(_A > _B) return 1;
else if(_A == _B) return 0;
else return -1;
}
});
Using 'store.fetch()' API while setting the 'sort' parameter, you control the order of returned items. The EntryDate you will have to create a comperatorMap of functions, as with Array.sort() in order to sort it properly. See the documentation.
var model = new dijit.tree.ForestStoreModel({
rootLabel: 'Names',
store: new dojo.data.ItemFileWriteStore({
data: {
identifier: 'rollNo',
items: data,
// blank, initially - can fill in with 'data' to show non-sorted items until sort is called
label: 'FirstName'
}
}) // blank itemsstore
});
var tree = new dijit.Tree({
model: model
});
OK, All set - but problem with .fetch is, it runs with callbacks (onComplete) and is difficult to control in a recursive manner. Instead, the functionality put in THIS FIDDLE duplicates the store data and then sorts it via native array sort - as opposed to using SimpleQueryEngine.
This will prove to give more reliable results - but does mess with DnD controllers and persist flag..
See how store can sort its items returned by fetch here: fiddle. This however only sorts one 'level' at a time and does not perform deep sorts.
IMO: The proper implementation of sort is a serverside sort, directly in the database query.
http://dojotoolkit.org/reference-guide/1.8/dojo/data/ItemFileReadStore.html#custom-sorting
http://dojotoolkit.org/reference-guide/1.8/dijit/Tree.html