Hi friends i have this array:
let array = [
{name:"a",age"b",id:1},
{name:"a",age"b",id:1},
{name:"a",age"b",id:1},
]
for insert the data in firebase i use this:
const saveItem = list => ref.child("story").push(list);
array.forEach(element => saveItem(element));
Is posibble insert without the forEeach ? (something like bulk in Mongo.)
saveItem(array);
You can do:
firebase.database().ref('story').push({
name: "a",
age: "20",
id : "1"
});
I'am assuming here that each user is entering there name, age and then clicking submit, in this case you have the following database:
story
randomId
name : a
age : 20
id : 1
randomId
name : b
age : 10
id : 2
Related
So I am a building a member system and every member has an array points.
But I can't seem to delete an object when a person tries to remove this point.
users(collection) -
member(document) -
"id" . --fields
"username" . --fields
"userevents" . --array
[0]
"id"
"date"
"price"
[1]
"id"
"date"
"price"
deletePoint = (userId, pointId, date, price) => {
let docRef = this.db.collection('users').doc(userId);
docRef.update({
points: this.db.FieldValue.arrayRemove({
date: date,
id: pointId,
price: price,
}),
});
};
this.db is firebase.firestore()
According to the documentation when you're using arrayRemove you have to point directly to the field that you want to remove, not the content within said field so your example would become:
deletePoint = (userId, pointId, date, price) => {
let docRef = this.db.collection('users').doc(userId);
docRef.update({
points: this.db.FieldValue.arrayRemove("0"),
});
};
And this will delete the whole content of the field (id, date, price) with an index of 0.
I want to upload a bunch of data into firebase database and set those data into a specific datatype.
In firebase database, we have string, number, boolean, map, array, null, timestamp, geopoint, and reference.
How do I get my flutter code to specify my database elements' types.
Future database() async {
return await userCollection.document(uid).setData({
string : ,
number : ,
boolean : ,
map : ,
array : ,
null : ,
timestamp : ,
geopoint : ,
reference: ,
});
}
setData takes a map as an argument, therefore you have to do the following:
Future database() async {
return await userCollection.document(uid).setData(
{
"name" : "john",
"age" : 50,
"email" : "example#example.com",
"address" : {
"street" : "street 24",
"city" : "new york"
}
})
This way you will add a string, integer, and a map to the database.
I have a list of employees array with {"id", "name", "designation"}, and now I have added a button to add some of them to managers array on clicking that button, all these arrays I have used via API. so how do we move one of the employees to managers array by clicking? I have tried something ... which was horribly wrong. so help me
HTML :
<div class="object center">
<h5>{{emp.name}}</h5>
<p>emp-id:{{emp.empid}}</p>
<button class="btn" (click)="onPush(emp.empid)">Make manager</button>
</div>
Component.ts
onPush(empid){
this.httpClient.put('http://localhost:3000/manager/' + empid)
.subscribe(success => {this.ngOnInit()}, error => {console.log("error")})
}
}
// this is my API
{
"employees": [
{
"empid": 214564567,
"name": "kolerag",
"designation":"lorem ipsum"
},
{
"empid": 214345546,
"name": "gunretro",
"designation":"lorem ipsum"
},
{
"empid": 2142225555,
"name": "trevanew",
"designation":"lorem ipsum"
},
],
"managers": []
}
So By clicking the button, the object from employees should append the
object to managers Array.enter image description here
Client side manipulation :
Assume data is the variable holding both employee and manager details.
data : {
employess : {
empid : number,
name : string,
designation : string
}[],
managers : {
empid : number,
name : string,
designation : string
}[]
}
onPush(id){
// Temporary variable
let temp : {
empid : number,
name : string,
designation : string
} = this.data.employees.find(emp=>emp.empid==id);
// Add to managers array
this.data.managers.push(temp);
// Slice from employees
this.data.employees = this.data.employees.filter(emp=>emp.empid!=id);
}
What's the cleanest and es6 native (if possible) way to loop through a object array to grab each unique value. Example would like this :
[{
"name" : "joe",
},
,{
"name" : "jean",
},
{
"name" : "joe",
},
{
"name" : "joe",
},
{
"name" : "mike",
}]
and in my results I want to see only : joe, jean, mike (only unique values, no dupes)
Since you mentioned ES6, it seems like a Set object would be what you want since it will do the uniqueness part for you and should do so fairly efficiently:
var objs = [{"name" : "joe"},{"name" : "jean"},{"name" : "joe"},{"name" : "joe"},{"name" : "mike"}];
let uniqueNames = Array.from(new Set(objs.map(item => item.name)));
console.log(uniqueNames);
Run this snippet to see the results.
a = [{name:"joe"},{name:"jean"},{name:"joe"},{name:"joe"},{name:"mike"}]
console.log(_.uniq(_.map(a, 'name'))) // Lodash 0.1.0
console.log([...new Set(a.map(o => o.name))]) // ES6
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.4/lodash.min.js"></script>
var objs = [{"name" : "joe"},{"name" : "jean"},{"name" : "joe"},{"name" : "joe"},{"name" : "mike"}];
var uniqueNames = objs.map( obj => obj.name )
.filter( (name, idx, arr) => { return arr.indexOf(name) === idx; } );
The .map extracts an array of the name values, and the .filter returns only the unique elements (first instances only).
I have a simple array with id and name. If i know the id then how can i fetch the name, array is shown below:
var dataobj = [
{id:1,name:"Jessica"},
{id:2,name:"Tom"},
{id:3,name:"Will"}
];
i have the id for example 2 with me in a variable, how can i get the name which belongs to this id ?
I have clickedID=2 value in my slist.component.ts and i want to fetch its corresponding name, how can i do it ?
To log the name which belongs to the id 2, it's as simple as following :
let obj = dataobj.find(obj => obj.id === 2);
console.log(obj.name);
you can use es6 array syntax:
dataobj.find(el => el.id === 2)
output:
Object {id: 2, name: "Tom"}
You can use the array find method
const secondItem = dataObj.find(function (item){
return item.id === 2;
})
Then name can be accessed as
secondItem.name
You can do something more readable and reusable with a dynamic find
var dataobj = [
{id:1,name:"Jessica"},
{id:2,name:"Tom"},
{id:3,name:"Will"}
];
let getNameFromObjId = obj => id => obj.find(x=> x.id===id).name;
console.log(getNameFromObjId(dataobj)(2))