This is a firbase response I get. My question is how do I get access to the bills? It is an list of objects.
{
"MYxaNHnKxPfdPlmwVGj": {
"bills": [
{"name": "Leo", "price": 23423},
{"name": "dds", "price": 3423},
{"name": "sdasd12312", "price": 12321}
]
},
"MYxia564oxQm-iYUMkO": {
"bills": [
{"name": "Leo", "price": 200}
]
}
}
You can do a foreach over the map and you will be able to get the key and value of the map.
void main() {
var usrMap = {
"MYxaNHnKxPfdPlmwVGj": {
"bills": [
{"name": "Leo", "price": 23423},
{"name": "dds", "price": 3423},
{"name": "sdasd12312", "price": 12321}
]
},
"MYxia564oxQm-iYUMkO": {
"bills": [
{"name": "Leo", "price": 200}
]
}
};
usrMap.forEach((k, v) {
print('Key: $k');
for(var bill in v['bills'] as List){
print('Name: ${bill['name']}');
print('Price: ${bill['price']}');
}
});
}
Related
Let's say I have a Customer document like the following
db.collection.insertOne( {
"customerName": "John Doe",
"orders": [
{
"type": "regular",
"items": [
{
"name": "itemA",
"price": 11.1
},
{
"name": "itemB",
"price": 22.2
}
]
},
{
"type": "express",
"items": [
{
"name": "itemC",
"price": 33.3
},
{
"name": "itemD",
"price": 44.4
}
]
}
]
})
How can I calculate the total price of all orders (111 in this example)?
You can $unwind twice (because nested array) and group using $sum like this:
db.collection.aggregate([
{
"$unwind": "$orders"
},
{
"$unwind": "$orders.items"
},
{
"$group": {
"_id": "$customerName",
"total": {
"$sum": "$orders.items.price"
}
}
}
])
Example here
I need to build the following json using groovy's JsonBuilder().
[
{
"date": "2017-12-08",
"dog": [
{
"name": "Joe",
"age": "1"
},
{
"name": "Bro",
"age": "2"
},
{
"name": "Doe",
"age": "3"
}
]
}
]
I cant get the array in the array, because of the date, it wants to always to put date not in the same level as dog array, but put it in { }, like:
[{
{
"date": "2017-12-08",
},
"dog": [
{
"name": "Joe",
"age": "1"
},
{
"name": "Bro",
"age": "2"
},
{
"name": "Doe",
"age": "3"
}
]
}
]
Just stick the date alongside your list in the model:
import groovy.json.JsonBuilder
import groovy.transform.Canonical
#Canonical
class Dog {
int age
String name
}
#Canonical
class DogList {
String date
List<Dog> dog
}
def ark = [
new DogList('2017-12-08', [
new Dog(1, 'Joe'),
new Dog(2, 'Bro'),
new Dog(3, 'Doe')
])
]
def json = new JsonBuilder(ark).toPrettyString()
println json
prints:
[
{
"date": "2017-12-08",
"dog": [
{
"age": 1,
"name": "Joe"
},
{
"age": 2,
"name": "Bro"
},
{
"age": 3,
"name": "Doe"
}
]
}
]
I'm assigned a task to get data using underscore js.
This is my JSON :
$scope.myData= {
"buslist":
{
"code":"1",
"message":"Success",
"fromStationCode":"71",
"searchResult": [
{
"arrivalTime": "17:00:00",
"availableSeats": "42",
"boardingPointDetails": [
{
"code": "1631",
"name": "Koyambedu",
"time": "09:30:00"
},
{
"code": "961296",
"name": "Nerkundram",
"time": "09:45:00"
}
]
},
{
"arrivalTime": "18:00:00",
"availableSeats": "32",
"boardingPointDetails": [
{
"code": "2084",
"name": "Adyar",
"time": "09:30:00"
},
{
"code": "961296",
"name": "Madurai",
"time": "09:45:00"
}
]
}
]
}
}
From this I want only "name" field. By doing this :
$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');
I got all "boardingPointDetails". The result looks like:
[ [
{
"code": "2084",
"name": "Adyar",
"time": "09:30:00"
},
{
"code": "961296",
"name": "Madurai",
"time": "09:45:00"
}
],[
{
"code": "1631",
"name": "Koyambedu",
"time": "09:30:00"
},
{
"code": "961296",
"name": "Nerkundram",
"time": "09:45:00"
}
],[
{
...
}
]
...
]
Help me to retrieve only "name" from this.
If you just want array of names like ['Koyambedu', 'Madurai'] then below code would work.
$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');
// Flatten the data.
$scope.flatbdata = _.flatten($scope.bdata, true);
$scope.flatbdata = $scope.flatbdata.filter(function(d){
return d != undefined && d.hasOwnProperty('name')
})
// map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results
$scope.names = $scope.flatbdata.map(function(d){
return d.name;
});
Refer below links :
http://underscorejs.org/#flatten
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
If you just want the array of names, here it is:
$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');
var res= _.flatten($scope.bdata);
res=_.pluck(res,'name');
console.log(res);
var temp = _.pluck(_.flatten($scope.bdata),'name')
temp will have ["Koyambedu", "Nerkundram", "Adyar", "Madurai"]
Try this.
$scope = {}; // in real project you don't need this. it's just for snippet.
$scope.myData = {
"buslist": {
"code": "1",
"message": "Success",
"fromStationCode": "71",
"searchResult": [{
"arrivalTime": "17:00:00",
"availableSeats": "42",
"boardingPointDetails": [{
"code": "1631",
"name": "Koyambedu",
"time": "09:30:00"
}, {
"code": "961296",
"name": "Nerkundram",
"time": "09:45:00"
}]
}, {
"arrivalTime": "18:00:00",
"availableSeats": "32",
"boardingPointDetails": [{
"code": "2084",
"name": "Adyar",
"time": "09:30:00"
}, {
"code": "961296",
"name": "Madurai",
"time": "09:45:00"
}]
}]
}
};
$scope.names = _.chain($scope.myData.buslist.searchResult).pluck("boardingPointDetails").flatten(true).map(function(item) {
return item.name;
}).value();
console.log($scope.names);
<script src="http://underscorejs.ru/underscore-min.js"></script>
I am new to working with JSON data. I am having difficulty on how to return back bicycles that have a color of "Blue". Using JSONPath.
The following example JSON file.
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
"bicycle": [
{
"price": 19.95
"color": [
"red"
],
},
{
"price": 20.99
"color": [
"blue",
"Green"
],
},
]
}
}
I have tried to use the following filter but it doesn't work.
$.store.bicycle[?(#.color=='blue')]
Any ideas to how to get this to work and only return the price of bicycles that are Blue?
Any information is greatly welcomed.
The JSON Data is
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}],
"bicycle": [
{
"price": 19.95,
"color": [
"red"
]
},
{
"price": 20.99,
"color": [
"blue",
"Green"
]
}
]
}
}
The JSONPath is
$.store.bicycle[?(#.color.indexOf('blue') != -1)]
Hi In my Controller from Server I am getting a JSON String
$scope.unsorted=data;
And the data looks like
[
{
"configId": 116,
"configName": "LAND_LINE",
"configFieldses": [
{
"FieldId": 784,
"FieldName": "engine.form.1",
"values": [
{
"keyname": "formName",
"value": "Account Number",
"id": 3068
},
{
"keyname": "formValuePosition",
"value": "right",
"id": 3069
}
]
},
{
"FieldId": 783,
"FieldName": "engine.form.0",
"values": [
{
"keyname": "formName",
"value": "Amount",
"id": 3074
},
{
"keyname": "formValuePosition",
"value": "right",
"id": 3075
},
{
"keyname": "regexGazatteer",
"value": "Total Charges :",
"id": 3076
}
]
},
{
"FieldId": 785,
"FieldName": "engine.table2",
"values": [
{
"keyname": "regexTableHeaderStart",
"value": null,
"id": 3079
},
{
"keyname": "regexTableBodyEnd",
"value": null,
"id": 3080
},
{
"keyname": "tableName",
"value": "invoice",
"id": 3078
}
]
}
]
}
]
I want to sort the configFieldses array based on FieldId and also the values array in each configFieldses based on id. So the JSON should looks like
[
{
"configId": 116,
"configName": "LAND_LINE",
"configFieldses": [
{
"FieldId": 783,
"FieldName": "engine.form.0",
"values": [
{
"keyname": "formName",
"value": "Amount",
"id": 3074
},
{
"keyname": "formValuePosition",
"value": "right",
"id": 3075
},
{
"keyname": "regexGazatteer",
"value": "Total Charges :",
"id": 3076
}
]
},
{
"FieldId": 784,
"FieldName": "engine.form.1",
"values": [
{
"keyname": "formName",
"value": "Account Number",
"id": 3068
},
{
"keyname": "formValuePosition",
"value": "right",
"id": 3069
}
]
},
{
"FieldId": 785,
"FieldName": "engine.table2",
"values": [
{
"keyname": "tableName",
"value": "invoice",
"id": 3078
}
{
"keyname": "regexTableHeaderStart",
"value": null,
"id": 3079
},
{
"keyname": "regexTableBodyEnd",
"value": null,
"id": 3080
}
]
}
]
}
]
What I have tried is first sort the parent array and then the child arrays.
var sortedfieldses = orderByFilter($scope.unsorted.configFieldses, '+FieldId');
But then How to remoive the old array from unsorted config and add the new to unsorted. And also how to loop through each and then sort it?
What is the best way?
Step 1: copy the object, then apply a custom sort function.
var sortedfieldses = angular.copy( $scope.unsorted.configFieldses );
sortedfieldses.sort( function( a, b ){
return a.FieldId > b.FieldId;
});
Step 2: Iterate through the "fieldses" and sort their children:
angular.forEach( sortedfieldses, function( field, key ){
field.values.sort( function( a, b ){
return a.id > b.id;
});
});
Actually re-reading your question, it sounds as though you just want to sort the 'unsorted' object in-place, rather than copying to a 'sorted' variable. If that's the case, skip the object copying and just do:
$scope.unsorted.configFieldses.sort( function( a, b ){
return a.FieldId > b.FieldId;
});
angular.forEach( $scope.unsorted.configFieldses, function( field, key ){
field.values.sort( function( a, b ){
return a.id > b.id;
});
});
The sort method alters the original array, rather than simply returning the sorted copy.
If you only need to sort the array, you could do this
$scope.unsorted=data;
// sort the parent
$scope.unsorted[0].configFieldses.sort(function(config1, config2) {
return config1.FieldId - config2.FieldId;
});
// sort the child values
$scope.unsorted[0].configFieldses.forEach(function(configField){
console.log(configField.values);
configField.values.sort(function(value1, value2) {
return value1.id - value2.id;
});
});
But if you want to display the the data using ng-repeat and orderBy filters, you could do use a nested ng-repeat like so.
<div ng-controller="ControllerX">
<div ng-repeat="configField in unsorted[0].configFieldses | orderBy:'FieldId' ">
<h3>{{configField.FieldId}}, {{configField.FieldName}}</h3>
<ul>
<li ng-repeat="value in configField.values | orderBy:'id'">
{{value.id}}, {{value.keyname}}
</li>
</ul>
</div>
</div>