I'm trying to modify ng-table to create expandable nodes/treeview,
Plunker example
The JSON structure is specified based on ID's.
[
{
"BodyId": 1,
"TrId": null,
"TdId": null,
"Title": "BodyId1",
"Status": "Ok"
}, {
"BodyId": 2,
"TrId": null,
"TdId": null,
"Title": "BodyId2",
"Status": "Ok"
}, {
"BodyId": 3,
"TrId": null,
"TdId": null,
"Title": "BodyId3",
"Status": "Ok"
}, {
"BodyId": 1,
"TrId": 1,
"TdId": null,
"Title": "TrId1",
"Status": "Ok"
}, {
"BodyId": 1,
"TrId": 2,
"TdId": null,
"Title": "TrId2",
"Status": "Ok"
}, {
"BodyId": 2,
"TrId": 1,
"TdId": null,
"Title": "TrId1",
"Status": "Ok"
}, {
"BodyId": 2,
"TrId": 2,
"TdId": null,
"Title": "TrId2",
"Status": "Ok"
}, {
"BodyId": 3,
"TrId": 1,
"TdId": null,
"Title": "TrId1",
"Status": "Ok"
}, {
"BodyId": 3,
"TrId": 2,
"TdId": null,
"Title": "TrId2",
"Status": "Ok"
}, {
"BodyId": 1,
"TrId": 1,
"TdId": 1,
"Title": "TdId1",
"Status": "Ok"
}, {
"BodyId": 1,
"TrId": 1,
"TdId": 2,
"Title": "TdId2",
"Status": "Ok"
}, {
"BodyId": 1,
"TrId": 2,
"TdId": "1",
"Title": "TdId1",
"Status": "Ok"
}, {
"BodyId": 2,
"TrId": 1,
"TdId": 1,
"Title": "TdId1",
"Status": "Ok"
}, {
"BodyId": 2,
"TrId": 1,
"TdId": 2,
"Title": "TdId2",
"Status": "Ok"
}, {
"BodyId": 2,
"TrId": 2,
"TdId": 1,
"Title": "TdId1",
"Status": "Ok"
}, {
"BodyId": 3,
"TrId": 3,
"TdId": 1,
"Title": "TdId1",
"Status": "Ok"
}]
I managed to display parent node by:
<tbody>
<tr ng-repeat="node in $data" ng-show="node.TrId == null && node.TdId == null">
<td ng-repeat="column in columns" ng-show="column.visible " sortable="column.field">
<span ng-show="column.field == 'Title'"><i class="glyphicon" ng-class="{'glyphicon-minus' : node.expanded, 'glyphicon-plus' : !node.expanded}"></i></span>
{{node[column.field]}}
</td>
</tr>
</tbody>
And here I'm stuck because I don't know how to attach below each parent node the corresponding child nodes and make all expandable after click.
Would be a good solution to parse this JSON structure and change it to JSON like would that make this problem easier to solve ?
[{
BodyId: 1,
TrID: [{
Id: 1,
TdId: [{
Id: 1,
Name: "Td1"
},
{
Id: 2,
Name: "Td2"
}]
}]
}]
Ok Guys so I have an answer for my own question, just in case if somebody need a tree view for ng-table solution is in here:
Plunker
<tr ng-repeat="user in $data">
<td ng-repeat="column in columns" ng-show="column.visible" sortable="column.field">
<div ng-click="isLevel1Collapsed = !isLevel1Collapsed">
<span ng-class="{'glyphicon glyphicon-plus':isLevel1Collapsed,'glyphicon glyphicon-minus': !isLevel1Collapsed}" ng-if="user.children.length > 0 && column.field === 'name'" ></span>
{{user[column.field]}}
</div>
<div ng-repeat="level1 in user.children">
<div ng-if="column.field === 'name'" collapse="isLevel1Collapsed">
<span style="margin-left: 20px;" ng-class="{'glyphicon glyphicon-plus':isLevel2Collapsed,'glyphicon glyphicon-minus': !isLevel2Collapsed}" ng-click="isLevel2Collapsed = !isLevel2Collapsed">
{{level1.jobTitle}}
</span>
<div collapse="isLevel2Collapsed">
<div ng-repeat="level2 in level1.children" ng-if="column.field === 'name'">
<span style="margin-left: 40px;">{{level2.car}}</span>
</div>
</div>
</div>
</div>
</td>
</tr>
JSON data structure:
[{
name: "Jacob",
age: 27,
children: [{
jobTitle: "Engineer2",
children: [{
car: "bmw1",
year: 2009
},
{
car: "audi",
year: 2010
}]
}]
},
{
name: "Enos",
age: 34,
children: [{
jobTitle: "Cheef",
children: [{
car: "bmw3",
year: 2009
},
{
car: "audi",
year: 2010
}]
}]
},
{
name: "Noea",
age: 43,
children: []
},
{
name: "Jacob",
age: 27,
children: [{
jobTitle: "Fireman",
children: [{
car: "bmw6",
year: 2009
},
{
car: "audi",
year: 2010
}]
}]
},
{
name: "Nephi",
age: 29,
children: [{
jobTitle: "Policeman",
children: [{
car: "bmw7",
year: 2009
},
{
car: "audi",
year: 2010
}]
}]
},
{
name: "Enos",
age: 34,
children: [{
jobTitle: "Bakerman",
children: [{
car: "bmw8",
year: 2009
},
{
car: "audi",
year: 2010
}]
}]
},
{
name: "Enos",
age: 34,
children: [{
jobTitle: "Teacher",
children: [{
car: "bmw11",
year: 2009
},
{
car: "audi",
year: 2010
}]
}]
}];
Related
I am trying to change the format of an array of objects based on a field called ParentID.
Here is a sample of data:
var JsonVal = "data": {
"Factorid": 197325,
"orders": [
{
"pID": 794522,
"Count": 1,
"ParentID": 794551,
"Description": "",
"productid": "1428539",
"UnitPrice": "300000",
},
{
"pID": 794525,
"Count": 1,
"ParentID": 794551,
"Description": "",
"productid": "1428543",
"UnitPrice": "600000",
},
{
"pID": 794550,
"Count": 2,
"ParentID": 0,
"Description": "",
"productid": "1428648",
"UnitPrice": "60000",
},
{
"pID": 794551,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428647",
"UnitPrice": "250000",
} ,
{
"pID": 794526,
"Count": 3,
"ParentID": 794550,
"Description": "",
"productid": "1428548",
"UnitPrice": "600000",
},
{
"pID": 794527,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428542",
"UnitPrice": "400000",
},
]
}
As you can see every object has a ParentID. I want to say that if ParentID is 0, it will be known as a parent and add a field call children which its value is null. However if ParentID is not 0 , it will mean that this Object is a child of another Object that its pID is the same as ParentID child Object.The final JSON should be like this :
"data": {
"Factorid": 197325,
"orders": [
{
"pID": 794550,
"Count": 2,
"ParentID": 0,
"Description": "",
"productid": "1428648",
"UnitPrice": "60000",
"children": "[{
'pID': 794526,
'Count':3,
'ParentID': 794550,
'Description': '',
'productid': '1428548',
'UnitPrice': '600000',
}]",
},
{
"pID": 794551,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428647",
"UnitPrice": "250000",
"children":"[
{
'pID': 794522,
'Count': 1,
'ParentID': 794551,
'Description': '',
'productid': '1428539',
'UnitPrice': '300000',
},
{
'pID': 794525,
'Count': 1,
'ParentID': 794551,
'Description': '',
'productid': '1428543',
'UnitPrice': '600000',
},
]",
} ,
{
"pID": 794527,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428542",
"UnitPrice": "400000",
"children":"",
},
]
}
children should be added to any object which is parent and also children is always a string field.
I wrote a code but I do not know how to handle it?
JsonVal.orders.forEach(orders => {
if(orders.ParentID == 0){
orders.children="";
}else{
if(orders.ParentID == new_rows){
JsonVal.orders = JsonVal.orders.filter(item => item.ParentID == orders.ParentID);
}
}
});
You basically filter your list of orders if it is a "rootOrder" which means that it has one or multiple children.
Next, you filter your list again by matching the ParentId to the root.pID
// input
const JsonVal = {
data: {
Factorid: 197325,
orders: [
{
pID: 794522,
Count: 1,
ParentID: 794551,
Description: "",
productid: "1428539",
UnitPrice: "300000",
},
{
pID: 794525,
Count: 1,
ParentID: 794551,
Description: "",
productid: "1428543",
UnitPrice: "600000",
},
{
pID: 794550,
Count: 2,
ParentID: 0,
Description: "",
productid: "1428648",
UnitPrice: "60000",
},
{
pID: 794551,
Count: 1,
ParentID: 0,
Description: "",
productid: "1428647",
UnitPrice: "250000",
},
{
pID: 794526,
Count: 3,
ParentID: 794550,
Description: "",
productid: "1428548",
UnitPrice: "600000",
},
{
pID: 794527,
Count: 1,
ParentID: 0,
Description: "",
productid: "1428542",
UnitPrice: "400000",
},
],
},
};
function mapOrders(orders) {
const rootOrders = orders.filter((order) => order.ParentID === 0);
return rootOrders.map((rootOrder) => {
const children = orders.filter((order) => order.ParentID === rootOrder.pID);
return {
...rootOrder,
children: children.length ? children : "",
};
});
}
// output
const mappedJsonVal = {
...JsonVal,
data: {
...JsonVal.data,
orders: mapOrders(JsonVal.data.orders),
},
};
Below is my response which is getting from the MySQL database,
I want to rearrange childInterest like in the second desired output
"childInterest": [
{
"id": 1,
"parentInterestId": 3,
"created_at": "2022-03-04T07:30:31.851Z",
"updated_at": "2022-03-04T07:30:31.851Z",
"interest": {
"id": 6,
"interestId": 3,
"name": "collecting stamps",
"level": 2,
"createdAt": "2022-01-27T14:22:22.586Z",
"updatedAt": "2022-01-27T14:22:22.586Z",
"heirarchy": {
"id": 3,
"interestId": null,
"name": "hobbies",
"level": 1,
"createdAt": "2022-01-27T14:20:40.362Z",
"updatedAt": "2022-01-27T14:20:40.362Z"
}
}
},
{
"id": 2,
"parentInterestId": 3,
"created_at": "2022-03-04T07:30:31.863Z",
"updated_at": "2022-03-04T07:30:31.863Z",
"interest": {
"id": 7,
"interestId": 3,
"name": "chess play",
"level": 2,
"createdAt": "2022-01-27T14:22:48.734Z",
"updatedAt": "2022-01-27T14:22:48.734Z",
"heirarchy": {
"id": 3,
"interestId": null,
"name": "hobbies",
"level": 1,
"createdAt": "2022-01-27T14:20:40.362Z",
"updatedAt": "2022-01-27T14:20:40.362Z"
}
}
},
{
"id": 5,
"parentInterestId": 4,
"created_at": "2022-03-04T08:06:03.499Z",
"updated_at": "2022-03-04T08:06:03.499Z",
"interest": {
"id": 8,
"interestId": 4,
"name": "cricket",
"level": 2,
"createdAt": "2022-01-27T14:23:13.042Z",
"updatedAt": "2022-01-27T14:23:13.042Z",
"heirarchy": {
"id": 4,
"interestId": null,
"name": "outdoor",
"level": 1,
"createdAt": "2022-01-27T14:21:00.701Z",
"updatedAt": "2022-01-27T14:21:00.701Z"
}
}
},
{
"id": 6,
"parentInterestId": 4,
"created_at": "2022-03-04T08:06:03.519Z",
"updated_at": "2022-03-04T08:06:03.519Z",
"interest": {
"id": 9,
"interestId": 4,
"name": "coco",
"level": 2,
"createdAt": "2022-01-27T14:23:22.940Z",
"updatedAt": "2022-01-27T14:23:22.940Z",
"heirarchy": {
"id": 4,
"interestId": null,
"name": "outdoor",
"level": 1,
"createdAt": "2022-01-27T14:21:00.701Z",
"updatedAt": "2022-01-27T14:21:00.701Z"
}
}
}
],
And I want a response like below
"childInterest": [
{
title: "Hobbies",
titleitems: [
"Music",
"Video"
]
},
{
title: "Indoor",
titleItems: [
"Carrom",
"Table Tenis"
]
},
{
title: "Outdoor",
titleItems: [
"Cricket",
"Kabbadi"
]
}
]
}
],
In 1st array whatever names hobbies, outdoor is there should come under single object inside that array should hobbies names like music video etc.
You probably want to take a look at the map() function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
My solution.
I won't be explaining steps by steps.
let childInterest = [
{
id: 1,
parentInterestId: 3,
created_at: "2022-03-04T07:30:31.851Z",
updated_at: "2022-03-04T07:30:31.851Z",
interest: {
id: 6,
interestId: 3,
name: "collecting stamps",
level: 2,
createdAt: "2022-01-27T14:22:22.586Z",
updatedAt: "2022-01-27T14:22:22.586Z",
heirarchy: {
id: 3,
interestId: null,
name: "hobbies",
level: 1,
createdAt: "2022-01-27T14:20:40.362Z",
updatedAt: "2022-01-27T14:20:40.362Z",
},
},
},
{
id: 2,
parentInterestId: 3,
created_at: "2022-03-04T07:30:31.863Z",
updated_at: "2022-03-04T07:30:31.863Z",
interest: {
id: 7,
interestId: 3,
name: "chess play",
level: 2,
createdAt: "2022-01-27T14:22:48.734Z",
updatedAt: "2022-01-27T14:22:48.734Z",
heirarchy: {
id: 3,
interestId: null,
name: "hobbies",
level: 1,
createdAt: "2022-01-27T14:20:40.362Z",
updatedAt: "2022-01-27T14:20:40.362Z",
},
},
},
{
id: 5,
parentInterestId: 4,
created_at: "2022-03-04T08:06:03.499Z",
updated_at: "2022-03-04T08:06:03.499Z",
interest: {
id: 8,
interestId: 4,
name: "cricket",
level: 2,
createdAt: "2022-01-27T14:23:13.042Z",
updatedAt: "2022-01-27T14:23:13.042Z",
heirarchy: {
id: 4,
interestId: null,
name: "outdoor",
level: 1,
createdAt: "2022-01-27T14:21:00.701Z",
updatedAt: "2022-01-27T14:21:00.701Z",
},
},
},
{
id: 6,
parentInterestId: 4,
created_at: "2022-03-04T08:06:03.519Z",
updated_at: "2022-03-04T08:06:03.519Z",
interest: {
id: 9,
interestId: 4,
name: "coco",
level: 2,
createdAt: "2022-01-27T14:23:22.940Z",
updatedAt: "2022-01-27T14:23:22.940Z",
heirarchy: {
id: 4,
interestId: null,
name: "outdoor",
level: 1,
createdAt: "2022-01-27T14:21:00.701Z",
updatedAt: "2022-01-27T14:21:00.701Z",
},
},
},
];
let newOne = Object.values(
childInterest.reduce(
(
acc,
{
interest: {
name: titleItems,
heirarchy: { name: title },
},
}
) => {
const key = [title];
acc[key] ||= { titleItems: [], title };
acc[key].titleItems.push(titleItems);
return acc;
},
{}
)
);
console.log(newOne);
I have an array of objects -
let initialArr = [
{
"id": 1,
"name": "product name",
"product_details": [
{
"id": 1,
"details": "some details"
}
],
"subscriptions": [
{
"id": 1,
"subs": "7 days"
},
{
"id": 2,
"subs": "15 days"
}
]
},
{
"id": 2,
"name": "product name 2",
"product_details": [
{
"id": 2,
"details": "some details 2"
}
],
"subscriptions": [
{
"id": 1,
"subs": "7 days"
},
{
"id": 2,
"subs": "15 days"
}
]
},
{
"id": 3,
"name": "product name 3",
"product_details": [
{
"id": 3,
"details": "some details 3"
}
],
"subscriptions": []
}
]
This is what I want to achieve -
[
{
"id": 1,
"name": "product name",
"detailsId" : 1,
"details": "some details"
"subsId": 1,
"subs": "7 days"
},
{
"id": 1,
"name": "product name",
"detailsId" : 1,
"details": "some details"
"subsId": 2,
"subs": "15 days"
},
{
"id": 2,
"name": "product name 2",
"detailsId" : 2,
"details": "some details 2"
"subsId": 1,
"subs": "7 days"
},
{
"id": 2,
"name": "product name 2",
"detailsId" : 2,
"details": "some details 2"
"subsId": 2,
"subs": "15 days"
},
{
"id": 3,
"name": "product name 3",
"detailsId" : 3,
"details": "some details 3"
}
]
This is what I have done -
initialArr.map(e => {
e.product_details.map(p =>{
e.subscriptions.map(s => {
newArr.push({
id: e.id,
name: e.name,
detailsId: p.id,
details: p.details,
subsId: s.id,
subs:s.subs
});
});
})
})
This works if subscriptions array is not empty. If for some product, subscriptions array is empty, then that product is not pushed into the array. In I am unable to figure out how to solve it.
3rd product is not pushed in the new array. This is what I get -
[
{
"id": 1,
"name": "product name",
"detailsId" : 1,
"details": "some details"
"subsId": 1,
"subs": "7 days"
},
{
"id": 1,
"name": "product name",
"detailsId" : 1,
"details": "some details"
"subsId": 2,
"subs": "15 days"
},
{
"id": 2,
"name": "product name 2",
"detailsId" : 2,
"details": "some details 2"
"subsId": 1,
"subs": "7 days"
},
{
"id": 2,
"name": "product name 2",
"detailsId" : 2,
"details": "some details 2"
"subsId": 2,
"subs": "15 days"
}
]
Note: Although the same product is repeated twice in the new array, but this is the requirement - products according to "subs" property of subscriptions array.
Supposing i had more arrays e.g. 'Customizations', 'Orders', etc. besides 'Subscriptions' and i want those arrays' data also to be pushed, is this the correct way to do for multiple arrays?
Assuming array product_details has allways 1 element with it's data this is a sollution.
Using Array#reduce to accumulate your new result-array. Creating foreach element a new temp-object with the data for all of it. If the subscriptions-array is empty push this temp-object to your accumulated result-array. Otherwise use Array#forEach to iterate over your subscriptions. For every subscription use Object.assign to make a copy of your temp-object. Add to this the subscription-data and push it to the result-array.
const initialArr = [{ id: 1, name: "product name", product_details: [{ id: 1, details: "some details" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 2, name: "product name 2", product_details: [{ id: 2, details: "some details 2" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 3, name: "product name 3", product_details: [{ id: 3, details: "some details 3" }], subscriptions: [] }];
let res = initialArr.reduce((acc, cur) => {
let temp = {
id: cur.id,
name: cur.name,
detailsId: cur.product_details[0].id,
details: cur.product_details[0].details
}
if (!cur.subscriptions.length)
acc.push(temp);
else {
cur.subscriptions.forEach(subs => {
let tempSub = Object.assign({}, temp);
tempSub.subsId = subs.id;
tempSub.subs = subs.subs;
acc.push(tempSub);
})
}
return acc;
}, []);
console.log(res);
Here a version without reduce and instead forEach:
const initialArr = [{ id: 1, name: "product name", product_details: [{ id: 1, details: "some details" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 2, name: "product name 2", product_details: [{ id: 2, details: "some details 2" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 3, name: "product name 3", product_details: [{ id: 3, details: "some details 3" }], subscriptions: [] }];
let acc = [];
initialArr.forEach(cur => {
let temp = {
id: cur.id,
name: cur.name,
detailsId: cur.product_details[0].id,
details: cur.product_details[0].details
}
if (!cur.subscriptions.length)
acc.push(temp);
else {
cur.subscriptions.forEach(subs => {
let tempSub = Object.assign({}, temp);
tempSub.subsId = subs.id;
tempSub.subs = subs.subs;
acc.push(tempSub);
})
}
});
console.log(acc);
You could check the length of subscription and return an object instead of mapping the array.
const
data = [{ id: 1, name: "product name", product_details: [{ id: 1, details: "some details" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 2, name: "product name 2", product_details: [{ id: 2, details: "some details 2" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 3, name: "product name 3", product_details: [{ id: 3, details: "some details 3" }], subscriptions: [] }],
result = data.flatMap(({ product_details: [{ id: detailsId, details }], subscriptions, ...o }) => subscriptions.length
? subscriptions.map(({ id: subsId, subs }) => ({ ...o, detailsId, details, subsId, subs }))
: ({ ...o, detailsId, details })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
you need to deal with the case explicitly:
if (e.subscriptions.length === 0) {
newArr.push({ ... }
} else {
e.subscriptions.map(...)
}
also you should use forEach instead of map.
I am looking to find a way to normalize nested many to many relationships.
However, it has been going when the model is a few levels deep, such as Categories in the example below. In the output the location id 27 only contains category ids 1 and 7 (which I assume is because that is what the last version of location 27 shows). What is the best approach in normalizing this type of data?
Would using shop id in the location be a good idea? i.e.
"locations": {
"1-27": {
"id": 27,
"label": "Birmingham",
"categories": [
1,
2,
7
]
},
"2-27": {
"id": 27,
"label": "Birmingham",
"categories": [
1,
7
]
}
},
Thanks for taking the time to read this!
Additional Data Details
Database
Shops
id
label
Locations
id
label
Location_Shop
location_id
shop_id
Categories
id
label
Category_Location
category_id
location_id
Example Data
[
{
id: 1,
label: 'First Shop',
locations: [
{
id: 27,
label: 'Birmingham',
categories: [
{
id: 1,
label: 'Car Park',
},
{
id: 2,
label: 'Petrol Station',
},
{
id: 7,
label: 'Bakery',
},
],
},
],
},
{
id: 2,
label: 'Second Shop',
locations: [
{
id: 27,
label: 'Birmingham',
categories: [
{
id: 1,
label: 'Car Park',
},
{
id: 7,
label: 'Bakery',
},
],
},
],
},
]
Code
const categorySchema = new schema.Entity('categories');
const locationSchema = new schema.Entity('locations', {
categories: [categorySchema],
});
const shopSchema = new schema.Entity('shops', {
locations: [locationSchema],
});
Output
{
"entities": {
"categories": {
"1": {
"id": 1,
"label": "Car Park"
},
"2": {
"id": 2,
"label": "Petrol Station"
},
"7": {
"id": 7,
"label": "Bakery"
}
},
"locations": {
"27": {
"id": 27,
"label": "Birmingham",
"categories": [
1,
7
]
}
},
"shops": {
"1": {
"id": 1,
"label": "First Shop",
"locations": [
27
]
},
"2": {
"id": 2,
"label": "Second Shop",
"locations": [
27
]
}
}
},
"result": [
1,
2
]
}
I have a JSON in this format:
JSON No 1
{
"status": "ok",
"data": [{
"id": 1,
"name": "building No1",
"floor": 5,
"code": {
"1": [{
"id": 1,
"code": "MCD-001",
"selected": false
}, {
"id": 2,
"code": "MCD-002",
"selected": false
}],
"2": [{
"id": 3,
"code": "MCD-003",
"selected": false
}],
"3": [{
"id": 4,
"code": "MCD-004",
"selected": false
}, {
"id": 5,
"code": "MCD-004-bis",
"selected": false
}, {
"id": 6,
"code": "MCD-005",
"selected": false
}],
"4": [{
"id": 7,
"code": "MCD-006",
"selected": false
}],
"6": [{
"id": 8,
"code": "MCD-007",
"selected": false
}],
"7": [{
"id": 9,
"code": "MCD-008",
"selected": false
}, {
"id": 10,
"code": "MCD-009",
"selected": false
}]
},
"building_name": "Test Tower",
"number_lot": true,
"parking_floor": 0,
"creation_date": {
"date": "2017-01-30 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}]
}
I have another JSON, something like this:
JSON No 2
{
"LOTS": {
"1": [{
"id": 1,
"code": "LOT-001",
"floor": 1,
"selected": true
}, {
"id": 2,
"code": "MCD-002",
"floor": 1,
"selected": true
}],
"7": [{
"id": 9,
"code": "MCD-008",
"floor": 7,
"selected": true
}, {
"id": 10,
"code": "MCD-009",
"floor": 7,
"selected": true
}]
}
}
Now what I want to set "selected":true in the JSON 1 key where code is MCD-008 and id:9
Does AngularJS provide someway to do either at template or controller end?
All I want to set button's classes to "default" or "primary". This thing is already being done. All I need to pass selected:true
You could use a hash table and het first the id and code from the source object and the interate the replacment object and set the values.
var data1 = { status: "ok", data: [{ id: 1, name: "building No1", floor: 5, code: { "1": [{ id: 1, code: "MCD-001", selected: false }, { id: 2, code: "MCD-002", selected: false }], "2": [{ id: 3, code: "MCD-003", selected: false }], "3": [{ id: 4, code: "MCD-004", selected: false }, { id: 5, code: "MCD-004-bis", selected: false }, { id: 6, code: "MCD-005", selected: false }], "4": [{ id: 7, code: "MCD-006", selected: false }], "6": [{ id: 8, code: "MCD-007", selected: false }], "7": [{ id: 9, code: "MCD-008", selected: false }, { id: 10, code: "MCD-009", selected: false }] }, building_name: "Test Tower", number_lot: true, parking_floor: 0, creation_date: { date: "2017-01-30 00:00:00.000000", timezone_type: 3, timezone: "UTC" } }] },
data2 = { LOTS: { "1": [{ id: 1, code: "LOT-001", floor: 1, selected: true }, { id: 2, code: "MCD-002", floor: 1, selected: true }], "7": [{ id: 9, code: "MCD-008", floor: 7, selected: true }, { id: 10, code: "MCD-009", floor: 7, selected: true }] } },
hash = Object.create(null);
data1.data.forEach(function (o) {
Object.keys(o.code).forEach(function (k) {
o.code[k].forEach(function (a) {
var key = [a.id, a.code].join('|');
hash[key] = a;
});
});
});
Object.keys(data2.LOTS).forEach(function (k) {
data2.LOTS[k].forEach(function (a) {
var key = [a.id, a.code].join('|');
if (hash[key]) {
hash[key].selected = a.selected;
}
});
});
console.log(data1);
.as-console-wrapper { max-height: 100% !important; top: 0; }