How to create nested json object in Angularjs - javascript

i want to create nested json object in angularjs. my object is this:
{
"marketerId": 1,
"baskets": [
{
"customer": {
"phone": ""
},
"region": 1,
"orders": [
{
"bookId": 1,
"count": 5
},
{
"bookId": 2,
"count": 52
}
]
},
{
"customer": {
"phone": ""
},
"region": 1,
"orders": [
{
"bookId": 1,
"count": 12
},
{
"bookId": 2,
"count": 2
}
]
}
]
}
For create this object as dynamically i write this code.Assuming orders and items already have been initialized, the form is created. For example, the size of the items and orders 2.Is there a better way to build nested json objects?
<input ng-model="formData.marketerId" />
<div class="row" ng-repeat="item in items track by $index">
<input ng-model="formData.baskets[$index].customer.phone" />
<input ng-model="formData.baskets[$index].region" />
<div ng-repeat="order in orders track by $index">
<input type="text" ng-model=
"formData.baskets[$parent.$index].orders[$index].bookId">
<input type="text" ng-model=
"formData.baskets[$parent.$index].orders[$index].count">
</div>
</div>

You can do something like this:
$scope.data1 = [];
var firstObj = new Object();
firstObj.first = "value1";
firstObj.second = "value2";
$scope.encountersData.push(firstObj);
$scope.data2 = [];
var secondObj= new Object();
secondObj.third = "value3";
secondObj.fourth = "value4";
$scope.data2.push(secondObj);

Related

How to filter an objects where is employeeId equals to some value

I'm using knockoutjs, but the question is really in Javascript domain.
I have variable vm.filteredSerivces() which contains all services by all employees.
Now, I want to just preserve those filteredSerivces where is vm.filteredSerivces()[0].GroupedServices[x].EmployeeId == 3684 (x is the number of index number of each object in GroupedServices object list)
I tried as follows:
var filteredSrvcs = vm.filteredSerivces()[0].GroupedServices.filter(x => x.EmployeeId != Id).remove();
vm.filteredSerivces(filteredSrvcs );
But I changed structure in that way, and my bindings in html is not corresponding.
Is there any other way to just remove this sub-sub object, and to preserve a structure as it is?
Here is the
Here's an example that maps a new array of new objects and the filter is set to only include the GroupedServices items where Id == 2000
let res = data.map(({ServiceTypeName, GroupedServices}) =>{
GroupedServices= GroupedServices.filter(({Id}) => Id == 2000);
return {ServiceTypeName,GroupedServices }
})
console.log(res)
<script>
let data =
[
{
"ServiceTypeName": "Type 1",
"GroupedServices": [{
"Id": 1,
"Name": "A"
}, {
"Id": 2,
"Name": "A"
},
{
"Id": 28456,
"Name": "AGSADGJS"
}]
},
{
"ServiceTypeName": "Type 2",
"GroupedServices": [{
"Id": 1203,
"Name": "AHASJ"
}, {
"Id": 2000,
"Name": "AHSJD"
},
{
"Id": 284536,
"Name": "UEHNCK"
}]
}];
</script>

How can I get unordered array of keys from object

I have data from backend in my js like this:
var list = {
"6": {
"id": 6,
"name": "John",
"age": 31
},
"42": {
"id": 42,
"name": "Alex",
"age": 25
},
"3": {
"id": 3,
"name": "Tim",
"age": 58
},
};
Then I need to display this data in my angular html template through ngFor directive. But first I have to get an array of object keys:
var listKeys= Object.keys(list);
Next I can output data in template:
<div *ngFor="let item of listKeys">
<p>{{list[item].id}}</p>
<p>{{list[item].name}}</p>
<p>{{list[item].age}}</p>
<hr>
</div>
But the problem is that order of my data changed. I have in listKeys next array ["3", "6", "42"]. But I want to have original order in that one ["6", "42", "3"]. One of solutions that I have found is make keys as not numeric string. For example:
var list = {
"+6": {...},
"+42": {...},
"+3": {...},
};
But I don't have access to backend. I need another solution.
P.S. The way in which I get data from the backend
getData() {
this._dataService.getList(this.name, this.age).subscribe(res => {
this.list = JSON.parse(JSON.stringify(res));
this.listKeys = Object.keys(this.list);
});
}
By definition, an object is an unordered collection of properties. As a solution, you could use an array instead of an object:
The first step would be to convert the response from the server to an array in the same order.
// Original JSON string received from API
var jsonString = `{
"6": {
"id": 6,
"name": "John",
"age": 31
},
"42": {
"id": 42,
"name": "Alex",
"age": 25
},
"3": {
"id": 3,
"name": "Tim",
"age": 58
}
}`;
// Array of ordered id's
const orderedIds = [];
// Find all id's in the JSON string and push them to the array
const pattern = /"?id"?\: (\d*)/g;
let match;
while (match = pattern.exec(jsonString)) {
orderedIds.push(parseInt(match[1]));
}
// parse the original JSON object
const originalList = JSON.parse(jsonString);
// resulting ordered Array
const result = [];
// Push the object in the array by order
for(x of orderedIds) {
result.push(originalList[x]);
}
// Log the resulting array
document.getElementById("result").innerText = JSON.stringify(result);
<pre id="result"></pre>
The result will be an array of the objects in the same order as they appeared in the JSON string:
result = [
{
"id": 6,
"name": "John",
"age": 31
},
{
"id": 42,
"name": "Alex",
"age": 25
},
{
"id": 3,
"name": "Tim",
"age": 58
},
];
After this you can use it in your template:
<div *ngFor="let item of result">
<p>{{item.id}}</p>
<p>{{item.name}}</p>
<p>{{item.age}}</p>
<hr>
</div>
this array does garantee the order of its values.
This is bound to have edge cases, but adding it because it works
If you are getting the data from the backend in the form of JSON then you can do the following
note: var json is a placeholder, as you haven't shown HOW you get your data
var json = `{
"6": {
"id": 6,
"name": "John",
"age": 31
},
"42": {
"id": 42,
"name": "Alex",
"age": 25
},
"3": {
"id": 3,
"name": "Tim",
"age": 58
}
}`;
var result = JSON.parse(json.replace(/\s?"(\d+)":/g, '"$1 ":'));
console.log(Object.keys(result));
Again, this is bound to fail, but I can't see any other way you can "fix" this on the client side - I thought JSON.parse "reviver" function would help, but it gets the properties in 3, 6, 42 order as well - so, no use at all

AngularJS use one object as a filter for another

I'm trying to filter my ng-repeat through a set of checkboxes which come from a different object. Object 1 holds my categories and object 2 holds all my articles.
The categores object will turn into checkboxes. These checkboxes should act as filter for the articles. An article can have mutliple categories.
$scope.categories:
[
{
"id": "1",
"title": "Blog"
},
{
"id": "2",
"title": "News"
}
]
$scope.articles:
[
{
"id": "1",
"title": "Whats going on",
"categories":{
"results" : [1,2,3]
}
},
{
"id": "2",
"title": "Trump!",
"categories":{
"results" : [3]
}
}
]
Checkboxes:
<div class="filter-pills" ng-repeat="cat in categories">
<input type="checkbox" ng-model="filter[cat.Id]" ng-checked="cat.checked"/>{{cat.Title}}
</div>
ng-repeat:
<div class="col-lg-3 col-md-4" ng-repeat="item in articlesFinal"></div>
I have tried different solutions like ng-change when i update my filter array and compare it to the object used in ng-repeat.
I can't seem to figure this one out. Any suggestions?
Try this
<div class="filter-pills" ng-repeat="cat in categories">
<input type="checkbox" ng-model="cat.checked"/>{{cat.title}}
</div>
<div class="col-lg-3 col-md-4" ng-repeat="item in articles | filter: catFilter">{{item.title}}</div>
and in controller
$scope.catFilter = function (article) {
var checkedCats = vm.categories.filter(function (cat) {
return cat.checked;
});
// no filter, show all
if(checkedCats.length == 0) return true;
for(var i = 0; i < checkedCats.length; i++){
var id = checkedCats[i].id;
if(article.categories.results.indexOf(id) >= 0){
return true;
}
}
// no match, then false
return false
};
Also notice that category id should be integer, not string
$scope.categories = [
{
"id": 1, // integer
"title": "Blog"
},
{
"id": 2,
"title": "News"
}
];
$scope.articles = [
{
"id": "1",
"title": "Whats going on",
"categories":{
"results" : [1,2,3] // integer
}
},
{
"id": "2",
"title": "Trump!",
"categories":{
"results" : [3]
}
}
];

why is ng-model not checking default radio button?

Below is the fiddle i am working:
http://jsfiddle.net/3c0dxf4d/
The ng-model has an object and the ng-value maps to object, why is my default value {"id":1,"name":"Bill"}
not getting selected by default.
Check out this fiddle http://jsfiddle.net/roz98eda/
var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
$scope.customers = [{
"id": 1,
"name": "Bill"
}, {
"id": 2,
"name": "Bob"
}, {
"id": 3,
"name": "Biff"
}];
$scope.customer = {};
$scope.currentCustomer = {
"id": 1
};
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="ctrl">
<table>
<tr ng-repeat="theCustomer in customers">
<td>
<input type="radio" ng-model="$parent.currentCustomer.id" ng-value="theCustomer.id">{{theCustomer.name}}</td>
</tr>
</table>
<br>
<div>{{currentCustomer}}</div>
</div>
</div>
Because you've put the initial value to
$scope.currentCustomer = {
"id": 1,
"name": "Bill"
};
Just remove or change it.
Please check following code please.
app.controller("ctrl", function ($scope) {
$scope.customers = [{
"id": 1,
"name": "Bill"
}, {
"id": 2,
"name": "Bob"
}, {
"id": 3,
"name": "Biff"
}];
$scope.customer = {};
*$scope.currentCustomer = {
"id": 1,
"name": "Bill"
};*
})
Change
<input type="radio" ng-model="$parent.currentCustomer" name="foo" ng-value="theCustomer" id="{{theCustomer.id}}">
To
<input type="radio" ng-model="$parent.currentCustomer.id" name="foo" ng-value="theCustomer.id" id="{{theCustomer.id}}">{{theCustomer.name}}</td>
From ng-value docs
It is mainly used on input[radio] and option elements, so that when
the element is selected, the ngModel of that element (or its select
parent element) is set to the bound value.

getting sum of grouped data angularjs - angular-filter

i am using angular-filter to group data. while i was able to group data and get data length(orderfood), i am not able to get sum of the qty in my grouped data. my plunk demo
the result i got
Isnain Meals - 2
Chicken Burger - 2
the result i need
Isnain Meals - 4 //sum of qty of Isnain Meals from JSON data (1+3)
Chicken Burger - 9 //sum of qty of Chicken Burger from JSON data (3+6)
JSON Data
$scope.orders = [{
"_id": "56b0c315e179bb0e00a44dbf",
"orderfood": [{
"_id": "569d865bff1fe20e00f8ba97",
"qty": "1",
"confirm": true,
"price": 154,
"name": "Isnain Meals"
}, {
"_id": "569d865bff1fe20e00f8ba98",
"qty": "3",
"confirm": true,
"price": 154,
"name": "Isnain Meals"
}],
"content": "9176649143",
"created": "2016-02-02T14:54:13.926Z"
}, {
"_id": "56b06ed25b53250e00ccbd73",
"orderfood": [{
"_id": "569d84f04834c10e003dff36",
"qty": "6",
"confirm": true,
"price": 125,
"name": "Chicken Burger"
}],
"content": "6886058585",
"created": "2016-02-02T08:54:42.986Z"
}, {
"_id": "56b06ed25b53250e00ccbd74",
"orderfood": [{
"_id": "569d84f04834c10e003dff37",
"qty": "3",
"confirm": true,
"price": 125,
"name": "Chicken Burger"
}],
"content": "6886058585",
"created": "2016-02-02T08:54:42.986Z"
}];
Controller Code
$scope.getOrderFoods = function() {
var orderfood = [];
angular.forEach($scope.orders, function(order) {
angular.forEach(order.orderfood, function(orderfoo) {
if (orderfood.indexOf(orderfoo) == -1) {
orderfood.push(orderfoo);
}
})
});
return orderfood;
}
HTML
<div ng-repeat="(key,data) in getOrderFoods() | groupBy:'name'">
<p>{{key}} - {{data.length}}</p>
<!-- instead of the data.length, i need the sum of qty -->
</div>
my plunk demo
You can use javascript Array.reduce method to generate the sum of quantity. here is the Plunk
<div ng-repeat="(key,data) in getOrderFoods() | groupBy:'name'">
<p>{{key}} - {{reduce(data)}}</p>
</div>
$scope.reduce= function(data){
return data.reduce(function(previousValue,currentValue, currentIndex, array){
return previousValue + parseInt(currentValue.qty);
}, 0);
}
In your solution used data.length that return array length not total qty because of groupBy generate an array based on groupBy conditioncan.
<div ng-repeat="(key,data) in getOrderFoods() | groupBy:'name'">
<p>{{key}} - {{data.length}}</p>
</div>
in this repeat generate two array with two items in each array because of you have two type item name and each type two times.
You can use another function to calculate total quantity that will call from in ng-repeat. In my solution used getTotalQuantity(data)
likeIn html:
<div ng-repeat="(key,data) in getOrderFoods() | groupBy:'name'">
<p>{{key}} - {{getTotalQuantity(data)}}</p>
</div>
in controller:
$scope.getTotalQuantity = function(items) {
var total =0;
angular.forEach(items, function(item) {
total+= parseInt(item.qty);
});
return total;
}

Categories

Resources