extract specific values from firebase database - reactjs

Given this database from firebase can someone tell me how to obtain the average score from the 2 data?
i have tried the following code but with no success. I believe the issue is on how to exactly extract the score value from the data.
CODE I HAVE TRIED
<h3>Average total: {Object.values(learnerObjects).reduce((a,b) => a + b, 0)/(learnerObjects.length || 1)}</h3>
DATABASE
learners
-MZKJfb-sDiNkSxFcqu4
email:
"pepe365#yahoo.com"
firstName:
"Gian"
lastName:
"Peppin"
score:
"10"
-MZN_V_YFt2naSReI4FA
email:
"may56#gmail.com"
firstName:
"feona"
lastName:
"may"
score:
"8"
{
"-MZKJfb-sDiNkSxFcqu4": {
"email": "pepe365#yahoo.com",
"firstName": "Gian",
"lastName": "Peppin",
"score": "10"
},
"-MZN_V_YFt2naSReI4FA": {
"email": "may56#gmail.com",
"firstName": "feona",
"lastName": "may",
"score": "8"
},
"-MZOBLhg1liCtmsG1M10": {
"email": "ciccio90#yahoo.com",
"firstName": "teo",
"lastName": "mammuccari",
"score": "7"
},
"-MZOBWPymXWdqjknKpfu": {
"email": "greenarr56#gmail.com",
"firstName": "mirko",
"lastName": "de luca",
"score": "5"
},
"-MZR2K_Cwkcqbz-UFlsN": {
"email": "fester80#yahoo.com",
"firstName": "Salvatore",
"lastName": "De Gennaro",
"score": "7"
},
"-MZR7AQggg222CYFFII_": {
"email": "pepti89#aol.com",
"firstName": "Nikhil",
"lastName": "Piparia",
"score": "6"
}
}

Related

How to write json in Angular getting different values

I know how to print this in Angular from a local jsonFile:
[{
"Firstname": "Steve",
"Lastname": "Jansson"
},
{
"Firstname": "Kurt",
"Lastname": "Nilsson"
},
{
"Firstname": "Lisa",
"Lastname": "Andersson"
}]
This is is how it looks when im printing it out
<p> List!</p>
<ul>
<li *ngFor="let adress of arrAdressbook; let i = index">{{adress.Firstname}} {{adress.Lastname}} </li>
</ul>
But how do I do if I want to print the following?
{
"Adressbook": [{
"Firstname": "Steve",
"Lastname": "Jansson",
"Adress": [{
"Street": "Hollowroad 17",
"City": "Sidney"
}]
},
{
"Firstname": "Kurt",
"Lastname": "Nilsson",
"Adress": [{
"Street": "Asbroad 14",
"City": "Canberra"
}]
},
{
"Firstname": "Lisa",
"Lastname": "Andersson",
"Adress": [{
"Street": "kangahoop 14",
"City": "Bunberry"
}]
}
]
}
Any help would be much appreciated!
Cheers! //Mcgajver
Much easier than you think. Use angular json pipe https://angular.io/api/common/JsonPipe
<pre>{{ data | json }}</pre>

How to fetch array of array data in angular 2?

How to fetch array of array data in angular 2.
My json data is as follows,
[[{
"pk_emp_id":5,
"tenant_id":"Zone1",
"location_id":1,
"emp_number":"sk44",
"prefix":"",
"first_name":"qqqqq",
"middle_name":"www",
"last_name":"eeee",
"display_name":"qqqq",
"full_name":"qqq qqqq",
"email":"qqqq#gmail.com",
"gender":"Female",
"emp_type_id":2,
"date_of_hire":191000,
"date_of_birth":null,
"manager_id":7,
"phone_number":"9877654",
"position":"SE",
"responsibility":"",
"notes":"",
"contracted":"0",
"street":"vidyanagar",
"state":"Karnataka",
"city":"hubli",
"zip_code":"9898",
"dob":-19800000,
"ssn":"",
"deleted":0
},{
"pk_empt_id":2,
"empt_tenant_id":"2",
"***empt_name***":"temporary",
"deleted":0
}]]
How can I fetch empt_name in datatable(row data)?
let dummyArr = [
[{
"pk_emp_id": 5,
"tenant_id": "Zone1",
"location_id": 1,
"emp_number": "sk44",
"prefix": "",
"first_name": "qqqqq",
"middle_name": "www",
"last_name": "eeee",
"display_name": "qqqq",
"full_name": "qqq qqqq",
"email": "qqqq#gmail.com",
"gender": "Female",
"emp_type_id": 2,
"date_of_hire": 191000,
"date_of_birth": null,
"manager_id": 7,
"phone_number": "9877654",
"position": "SE",
"responsibility": "",
"notes": "",
"contracted": "0",
"street": "vidyanagar",
"state": "Karnataka",
"city": "hubli",
"zip_code": "9898",
"dob": -19800000,
"ssn": "",
"deleted": 0
}, {
"pk_empt_id": 2,
"empt_tenant_id": "2",
"***empt_name***": "temporary",
"deleted": 0
}]
];
dummyArr.forEach(element =>{
element.forEach(element2=>{
console.log(element2)
})
});
I believe you have exposed a GET method in your web api
In Angular using HTTP service
this.http.get("").subscribe(result => {
conosle.log(result[1].empt_name) });
You can take it in an array and read it like this:
x = [
[{
"pk_emp_id": 5,
"tenant_id": "Zone1",
"location_id": 1,
"emp_number": "sk44",
"prefix": "",
"first_name": "qqqqq",
"middle_name": "www",
"last_name": "eeee",
"display_name": "qqqq",
"full_name": "qqq qqqq",
"email": "qqqq#gmail.com",
"gender": "Female",
"emp_type_id": 2,
"date_of_hire": 191000,
"date_of_birth": null,
"manager_id": 7,
"phone_number": "9877654",
"position": "SE",
"responsibility": "",
"notes": "",
"contracted": "0",
"street": "vidyanagar",
"state": "Karnataka",
"city": "hubli",
"zip_code": "9898",
"dob": -19800000,
"ssn": "",
"deleted": 0
}, {
"pk_empt_id": 2,
"empt_tenant_id": "2",
"***empt_name***": "temporary",
"deleted": 0
}]
]
$('#a').text(x[0][1]['***empt_name***'])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<span id='a'>sadsad </span>
This is just Javascript object manipulation.
You can do something like :
outerArray.forEach(innerArray =>{
if(innerArray.length){
innerArray.forEach(element =>{
//do somthing here with element.empt_name
console.log(element.empt_name);
});
}
})

Angular Differences Between Two Models

I have an angular application which uses a significantly large shared model. Currently, when a user presses save the entire model is posted to a RESTful service. Ideally I would only like to post the fields that have changed. Since this is a shared model I do not have access to form validation states such as dirty/pristine etc. The idea I can think of is to have two models, the original and the modified and compare these.
Original Model
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"id": "123",
"number": "212 555-1234"
},
{
"id": "456",
"number": "646 555-4567"
},
{
"id": "789",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
Changed Model
{
"firstName": "Jane",
"lastName": "Smith",
"isAlive": true,
"age": 50,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"id": "123",
"number": "1234567890"
},
{
"id": "456",
"number": "646 555-4567"
},
{
"id": "789",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
Data Posted - This is what I need!
{
"firstName": "Jane",
"age": 50,
"phoneNumbers": [
{
"id":"123",
"number": "1234567890"
}
]
}
How can I achieve this? I need the changed fields including any fields called id!
You'll need something like this. Working Fiddle: https://jsfiddle.net/jyyyLaot/
function filter(obj1, obj2) {
var result = {};
for(key in obj1) {
if(obj2[key] != obj1[key]) result[key] = obj2[key];
if(typeof obj2[key] == 'array' && typeof obj1[key] == 'array')
result[key] = arguments.callee(obj1[key], obj2[key]);
if(typeof obj2[key] == 'object' && typeof obj1[key] == 'object')
result[key] = arguments.callee(obj1[key], obj2[key]);
}
return result;
}

Get Value After Payment

I have the following object. How do I get the email value from it?
{
"payment_method": "paypal",
"payer_info": {
"email": "example#domain.com",
"first_name": "example",
"last_name": "xxx",
"payer_id": "12313213",
"shipping_address": {
"recipient_name": "Example",
"id": "5435345",
"line1": "1 SS ",
"city": "San Jose",
"state": "CA",
"postal_code": "95131",
"country_code": "US"
},
"phone": "4547567",
"country_code": "US"
}
}

Searching tag in object Angularjs

I have an tag array list which user enters
$scope.tags = ["INDIA","USA","JAPAN","CHINA"];
$scope.object = [ {
"name": "Executive",
"FirstName": "Jackey",
"LastName": "Gordon",
"Title": null,
"email": "admin#admin.com",
"place" : "USA"
},
{
"name": "Executive",
"FirstName": "Jackey",
"LastName": "Gordon",
"Title": null,
"email": "admin#admin.com",
"place" : "INDIA"
},
{
"name": "Executive",
"FirstName": "Jackey",
"LastName": "Gordon",
"Title": null,
"email": "admin#admin.com",
"place" : "INDIA"
},
{
"name": "Executive",
"FirstName": "Jackey",
"LastName": "Gordon",
"Title": null,
"email": "admin#admin.com",
"place" : "AUSTRALIA"
},
{
"name": "Executive",
"FirstName": "Jackey",
"LastName": "Gordon",
"Title": null,
"email": "admin#admin.com",
"place" : "SOUTHAFRICA"
} ]
I need to search $scope.tags i.e INDIA,USA,CHINA,JAPAN in $scope.object and return new array.
So new array object will be like
$scope.new = [ {
"name": "Executive",
"FirstName": "Jackey",
"LastName": "Gordon",
"Title": null,
"email": "admin#admin.com",
"place" : "USA"
},
{
"name": "Executive",
"FirstName": "Jackey",
"LastName": "Gordon",
"Title": null,
"email": "admin#admin.com",
"place" : "INDIA"
},
{
"name": "Executive",
"FirstName": "Jackey",
"LastName": "Gordon",
"Title": null,
"email": "admin#admin.com",
"place" : "INDIA"
}]
you can try something like this
$scope.newArr = [];
angular.forEach($scope.object , function(val,key) {
var exists = ($scope.tags).indexOf(val.place);
if(exists >= 0) {
$scope.newArr.push(val);
}
});
here is the working plunker
Try this, I didn't test.
$scope.newObj;
for (var i = 0; i < $scope.tags.length; i++) {
for (var j = 0; j < $scope.object.length; j++) {
if ($scope.tags[i].toLowerCase() == $scope.object[j].place.toLowerCase()) {
$scope.newObj.push($scope.object[j]);
}
}
}

Resources