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

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.

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 filter object in realm?

I am working on a react native project and I am using realm to store data in local database to my object is like bellow
[
{
title: 'title1',
data: [{name: 'name1', city: 'paris'},{name: 'name2', city: madrid}]
},
{
title: 'title2',
data: [{name: 'name4', city: 'londer'},{name: 'name6', city: roma}]
}
]
my question is how can i filter by data properties so for example select only the object that in their data there is an element city === 'londer'
You can do something like:
Realm.open({schema:[*Your_Schema*]}).then(realm => const data = realm.objects('*Your_Type*').filtered('*Your_Condition*'));
Check this link for more details.

Express configure doesn't work

I use
app.configure( function(){
app.use(express.static(__dirname, '/'));
});
then I figured out it's the old version. Then I used this,
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
app.use(express.static(__dirname, '/'));
}
but it still give me errors. My whole code is below.
var express = require('express'),
app = express();
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
app.use(express.static(__dirname, '/'));
}
app.get('/people/:id', function(req, res){`
var customerId = parseInt(req.params.id);
var data = {};
for(var i = 0; i < people.length; i++){
if(people[i].id === customerId){
data = people[i];
break;
}
}
res.json(data);
});
app.get('/people', function(req, res){
res.json(people);
});
app.lisen(8080);
console.log('Listening on express port 8080');
var people = [
{id: 1, name: 'John', city: 'Lisbon', gender: 'male', total: '515.561',
orders: [
{id: 1, product: 'Shoes', total: '100'}
]
},
{id: 2, name:'Abbie', city:'Orlando', gender:'female', total:'3445.34',
orders: [
{id: 2, product: 'Shoes', total: '200.561'}
]
},
{id: 3, name:'Will', city:'Houston', gender:'female', total:'98754.00',
orders: [
{id: 1, product: 'Shoes', total: '300.561'},
{id: 3, product: 'Shoes', total: '330.561'}
]
},
{id: 4, name:'Jim', city:'Paris', gender:'male', total:'15.26',
orders: [
{id: 4, product: 'Shoes', total: '400.561'}
]
},
{id: 5, name:'Bryan', city:'Lisbon', gender:'male', total:'515.561',
orders: [
{id: 5, product: 'Shoes', total: '500.561'}
]
},
{id: 6, name:'Agulera', city:'Orlando', gender:'female', total:'3445.34',
orders: [
{id: 6, product: 'Shoes', total: '600.561'}
]
},
{id: 7, name:'Christeen', city:'Houston', gender:'female', total:'98754.00',
orders: [
{id: 7, product: 'Shoes', total: '700.561'}
]
},
{id: 8, name:'Matt', city:'Paris', gender:'male',total:'15.26',
orders: [
{id: 8, product: 'Shoes', total: '800.561'}
]
}
];
I'm quite new to express and I'm doing this by watching a video. so I want to configure it properly and run it. Please help me with this.
spelling error
app.lisen(8080);
--->
app.listen(8080);
second...
global variables are bad strategy, put the var people as a local variable of app:
http://expressjs.com/api.html#app.locals
app.locals.people = "put the people data here";
Lastly, you should put responses and relevant http error codes to diagnose where there are errors:
for example.....
var yes = "yes";
if ( yes !== data.answer) {
console.log(err);
res.status(401).send(err + "error, you do not have authorized access to this data"); }
else {
console.log("success! You are authorized!")
res.status(200).send(data);
};
Sends back json object data and "ok" status code if condition is met;
status codes:
http://www.w3.org/Protocols/HTTP/HTRESP.html

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