Loop through object javascript - javascript

This is my response body. I want to select only Collaborator's "supportNotifications" key-value set to true, how can I do that?
{
"status": true,
"date": "2022-04-06T16:33:13.630Z",
"data": {
"collaborators": [
{
"name": "Paul",
"lastCheckin": "2022-03-31T19:54:50.584Z",
"sales": 1,
"createdAt": "2022-03-30T14:47:48.478Z",
"id": "62446d4ab7f4da001e8f40dc",
"supportNotification": true,
"collaboratorId": 1
},
{
"name": "John",
"lastCheckin": null,
"sales": 0,
"createdAt": "2022-03-01",
"id": "62446ea4b7f4da001e8f40df",
"supportNotification": false,
"collaboratorId": 6
}
],
"count": 2
}
}

let response = {
"status": true,
"date": "2022-04-06T16:33:13.630Z",
"data": {
"collaborators": [
{
"name": "Paul",
"lastCheckin": "2022-03-31T19:54:50.584Z",
"sales": 1,
"createdAt": "2022-03-30T14:47:48.478Z",
"id": "62446d4ab7f4da001e8f40dc",
"supportNotification": true,
"collaboratorId": 1
},
{
"name": "John",
"lastCheckin": null,
"sales": 0,
"createdAt": "2022-03-01",
"id": "62446ea4b7f4da001e8f40df",
"supportNotification": false,
"collaboratorId": 6
}
],
"count": 2
}
};
const collaborators = response.data.collaborators.filter(c => c.supportNotification);

This will give you each collaborator object that has supportNotification = true. Is this the result you are trying to select?
var body = {
status: true,
date: "2022-04-06T16:33:13.630Z",
data: {
collaborators: [
{
name: "Paul",
lastCheckin: "2022-03-31T19:54:50.584Z",
sales: 1,
createdAt: "2022-03-30T14:47:48.478Z",
id: "62446d4ab7f4da001e8f40dc",
supportNotification: true,
collaboratorId: 1,
},
{
name: "John",
lastCheckin: null,
sales: 0,
createdAt: "2022-03-01",
id: "62446ea4b7f4da001e8f40df",
supportNotification: false,
collaboratorId: 6,
},
],
count: 2,
},
};
var result = [];
body.data.collaborators.forEach((item) => {
if (item.supportNotification === true) {
result.push(item);
}
});
console.log(result);

Related

Counting all nested objects in array where selected attribute is true

I need to create a recursive function counting objects nested in the array where the selected attribute is true.
var data = [{"id":1,"code":"1","selected":false,"children":[{"id":4,"code":"1.01","selected":false,"children":[{"id":5,"code":"1.01.001","selected":true,"children":[]},{"id":6,"code":"1.01.002","selected":false,"children":[]},{"id":20,"code":"1.01.003","selected":true,"children":[]}]}]},{"id":2,"code":"2","selected":false,"children":[{"id":7,"code":"2.01","selected":false,"children":[{"id":9,"code":"2.01.001","selected":true,"children":[]},{"id":21,"code":"2.01.002","selected":true,"children":[]},{"id":22,"code":"2.01.003","selected":false,"children":[]}]}]},{"id":3,"code":"3","selected":false,"children":[{"id":8,"code":"3.01","selected":false,"children":[{"id":10,"code":"3.01.01","name":"Sementes","selected":false,"children":[{"id":11,"code":"3.01.01.001","selected":true,"children":[]},{"id":23,"code":"3.01.01.002","selected":false,"children":[]},{"id":24,"code":"3.01.01.003","selected":true,"children":[]}]},{"id":25,"code":"3.01.02","selected":false,"children":[{"id":27,"code":"3.01.02.001","selected":true,"children":[]},{"id":28,"code":"3.01.02.002","selected":false,"children":[]},{"id":29,"code":"3.01.02.003","selected":false,"children":[]}]},{"id":26,"code":"3.01.03","selected":false,"children":[{"id":30,"code":"3.01.03.001","selected":true,"children":[]},{"id":31,"code":"3.01.03.002","selected":true,"children":[]},{"id":32,"code":"3.01.03.003","selected":true,"children":[]},{"id":35,"code":"3.01.03.004","selected":false,"children":[]},{"id":34,"code":"3.01.03.005","selected":false,"children":[]}]}]}]}];
const countSelectedChildren = (arr) => {
return arr;
}
console.log(countSelectedChildren(data))
Expected response:
{
"id": 3,
"code": "3",
"selected": false,
"selectedChildren": 6,
"children": [
{
"id": 8,
"code": "3.01",
"selected": false,
"selectedChildren": 6,
"children": [
{
"id": 10,
"code": "3.01.01",
"name": "Sementes",
"selected": false,
"selectedChildren": 2,
"children": [
{
"id": 11,
"code": "3.01.01.001",
"selected": true,
"children": []
},
{
"id": 23,
"code": "3.01.01.002",
"selected": false,
"children": []
},
{
"id": 24,
"code": "3.01.01.003",
"selected": true,
"children": []
}
]
},
{
"id": 25,
"code": "3.01.02",
"selected": false,
"selectedChildren": 1,
"children": [
{
"id": 27,
"code": "3.01.02.001",
"selected": true,
"children": []
},
{
"id": 28,
"code": "3.01.02.002",
"selected": false,
"children": []
},
{
"id": 29,
"code": "3.01.02.003",
"selected": false,
"children": []
}
]
},
{
"id": 26,
"code": "3.01.03",
"selected": false,
"selectedChildren": 3,
"children": [
{
"id": 30,
"code": "3.01.03.001",
"selected": true,
"children": []
},
{
"id": 31,
"code": "3.01.03.002",
"selected": true,
"children": []
},
{
"id": 32,
"code": "3.01.03.003",
"selected": true,
"children": []
},
{
"id": 35,
"code": "3.01.03.004",
"selected": false,
"children": []
},
{
"id": 34,
"code": "3.01.03.005",
"selected": false,
"children": []
}
]
}
]
}
]
}
Can you help me to create this recursive function?
const countSelectedChildren = (arr) => {
return arr;
}
Thanks for your help!
You could take a recursive function for an array and return an object with a counting property and a children array.
const
addSelected = array => {
let selectedChildren = 0;
const
children = array.map(({ children, ...o }) => {
if (o.selected) selectedChildren++;
const temp = addSelected(children);
selectedChildren += temp.selectedChildren || 0;
return { ...o, ...temp };
});
return children.length
? { selectedChildren, children }
: { children };
},
data = [{ id: 1, code: "1", selected: false, children: [{ id: 4, code: "1.01", selected: false, children: [{ id: 5, code: "1.01.001", selected: true, children: [] }, { id: 6, code: "1.01.002", selected: false, children: [] }, { id: 20, code: "1.01.003", selected: true, children: [] }] }] }, { id: 2, code: "2", selected: false, children: [{ id: 7, code: "2.01", selected: false, children: [{ id: 9, code: "2.01.001", selected: true, children: [] }, { id: 21, code: "2.01.002", selected: true, children: [] }, { id: 22, code: "2.01.003", selected: false, children: [] }] }] }, { id: 3, code: "3", selected: false, children: [{ id: 8, code: "3.01", selected: false, children: [{ id: 10, code: "3.01.01", name: "Sementes", selected: false, children: [{ id: 11, code: "3.01.01.001", selected: true, children: [] }, { id: 23, code: "3.01.01.002", selected: false, children: [] }, { id: 24, code: "3.01.01.003", selected: true, children: [] }] }, { id: 25, code: "3.01.02", selected: false, children: [{ id: 27, code: "3.01.02.001", selected: true, children: [] }, { id: 28, code: "3.01.02.002", selected: false, children: [] }, { id: 29, code: "3.01.02.003", selected: false, children: [] }] }, { id: 26, code: "3.01.03", selected: false, children: [{ id: 30, code: "3.01.03.001", selected: true, children: [] }, { id: 31, code: "3.01.03.002", selected: true, children: [] }, { id: 32, code: "3.01.03.003", selected: true, children: [] }, { id: 35, code: "3.01.03.004", selected: false, children: [] }, { id: 34, code: "3.01.03.005", selected: false, children: [] }] }] }] }],
result = addSelected(data).children;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Here's a fairly simple recursion to do this:
const sum = (ns) => ns .reduce ((a, b) => a + b, 0)
const countSelectedChildren = (xs) =>
xs .map (({children = [], ...rest}, _, __, kids = countSelectedChildren (children)) => ({
...rest,
...(children .length
? {selectedChildren: sum (kids .map (x => (x .selected ? 1 : 0) + (x .selectedChildren || 0)))}
: {}
),
children: kids,
}))
const data = [{"id":1,"code":"1","selected":false,"children":[{"id":4,"code":"1.01","selected":false,"children":[{"id":5,"code":"1.01.001","selected":true,"children":[]},{"id":6,"code":"1.01.002","selected":false,"children":[]},{"id":20,"code":"1.01.003","selected":true,"children":[]}]}]},{"id":2,"code":"2","selected":false,"children":[{"id":7,"code":"2.01","selected":false,"children":[{"id":9,"code":"2.01.001","selected":true,"children":[]},{"id":21,"code":"2.01.002","selected":true,"children":[]},{"id":22,"code":"2.01.003","selected":false,"children":[]}]}]},{"id":3,"code":"3","selected":false,"children":[{"id":8,"code":"3.01","selected":false,"children":[{"id":10,"code":"3.01.01","name":"Sementes","selected":false,"children":[{"id":11,"code":"3.01.01.001","selected":true,"children":[]},{"id":23,"code":"3.01.01.002","selected":false,"children":[]},{"id":24,"code":"3.01.01.003","selected":true,"children":[]}]},{"id":25,"code":"3.01.02","selected":false,"children":[{"id":27,"code":"3.01.02.001","selected":true,"children":[]},{"id":28,"code":"3.01.02.002","selected":false,"children":[]},{"id":29,"code":"3.01.02.003","selected":false,"children":[]}]},{"id":26,"code":"3.01.03","selected":false,"children":[{"id":30,"code":"3.01.03.001","selected":true,"children":[]},{"id":31,"code":"3.01.03.002","selected":true,"children":[]},{"id":32,"code":"3.01.03.003","selected":true,"children":[]},{"id":35,"code":"3.01.03.004","selected":false,"children":[]},{"id":34,"code":"3.01.03.005","selected":false,"children":[]}]}]}]}];
console .log (countSelectedChildren (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
For each element in the input array recur first on any children, then to calculate the selectedChildren for our current node we sum up the results from each of our children, adding one for each if it's selected. Then we simply put back together a new object with selectedChildren included if we have actual children, with children the result of our recursion, and with the rest of the properties from our element.
While we could inline the one call to the sum helper function, it's something we're likely to want fairly often, so it's cleaner to keep it separate.
const countSelectedChildren = (data) => {
let length = 0;
for (item of data) {
item.selected && (length++)
let child_length = countSelectedChildren(item.children || [])[1];
length += child_length;
item.selectedChildren = child_length;
}
return [data, length];
}
I would approach it like this
const countSelectedChildren = (inArr) => {
const outArr = [];
inArr.forEach(row => {
// simply count all chilren with selected=true
row.SelectedChildren = row.children.filter(row => row.selected).length;
// if there are children, call this function on them
if (row.children.length) {
row.children = countSelectedChildren(row.children);
}
outArr.push(row);
});
return outArr;
}
const data = [
{
"id": 1,
"code": "1",
"selected": false,
"children": [
{
"id": 4,
"code": "1.01",
"selected": false,
"children": [
{
"id": 5,
"code": "1.01.001",
"selected": true,
"children": []
},
{
"id": 6,
"code": "1.01.002",
"selected": false,
"children": []
},
{
"id": 20,
"code": "1.01.003",
"selected": true,
"children": []
}
]
}
]
},
{
"id": 2,
"code": "2",
"selected": false,
"children": [
{
"id": 7,
"code": "2.01",
"selected": false,
"children": [
{
"id": 9,
"code": "2.01.001",
"selected": true,
"children": []
},
{
"id": 21,
"code": "2.01.002",
"selected": true,
"children": []
},
{
"id": 22,
"code": "2.01.003",
"selected": false,
"children": []
}
]
}
]
},
{
"id": 3,
"code": "3",
"selected": false,
"children": [
{
"id": 8,
"code": "3.01",
"selected": false,
"children": [
{
"id": 10,
"code": "3.01.01",
"name": "Sementes",
"selected": false,
"children": [
{
"id": 11,
"code": "3.01.01.001",
"selected": true,
"children": []
},
{
"id": 23,
"code": "3.01.01.002",
"selected": false,
"children": []
},
{
"id": 24,
"code": "3.01.01.003",
"selected": true,
"children": []
}
]
},
{
"id": 25,
"code": "3.01.02",
"selected": false,
"children": [
{
"id": 27,
"code": "3.01.02.001",
"selected": true,
"children": []
},
{
"id": 28,
"code": "3.01.02.002",
"selected": false,
"children": []
},
{
"id": 29,
"code": "3.01.02.003",
"selected": false,
"children": []
}
]
},
{
"id": 26,
"code": "3.01.03",
"selected": false,
"children": [
{
"id": 30,
"code": "3.01.03.001",
"selected": true,
"children": []
},
{
"id": 31,
"code": "3.01.03.002",
"selected": true,
"children": []
},
{
"id": 32,
"code": "3.01.03.003",
"selected": true,
"children": []
},
{
"id": 35,
"code": "3.01.03.004",
"selected": false,
"children": []
},
{
"id": 34,
"code": "3.01.03.005",
"selected": false,
"children": []
}
]
}
]
}
]
}
];
const countSelectedChildren = (inArr) => {
const outArr = [];
inArr.forEach(row => {
// simply count all chilren with selected=true
row.SelectedChildren = row.children.filter(row => row.selected).length;
// if there are children, call this function on them
if (row.children.length) {
row.children = countSelectedChildren(row.children);
}
outArr.push(row);
});
return outArr;
}
console.log(countSelectedChildren(data))

Filter complex array in react native?

I have array .
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}]
I tried following the code to filter it. and no output show
arr.map((item,idx) => (
console.log(item.data.games.gamename)
)
))
I want to print all game name eg.
cricket
football
videogames
volleyball
We can use flatMap() to do it
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}]
// multiple flatMap chain invocation seems ugly,waiting for more elegant solution
let result = arr.flatMap(a => a.data).flatMap(a => a.games).flatMap(a => a.gamename)
console.log(result)
Data is a array and so is games:
const arr = [
{
status: "success",
data: [
{
name: "user1",
games: [
{
id: 1,
gamename: "cricket",
},
{
id: 2,
gamename: "football",
},
],
},
{
name: "user1",
games: [
{
id: 1,
gamename: "videogames",
},
{
id: 2,
gamename: "volleyball",
},
],
},
],
},
];
arr.map((item) => {
item.data.map((item) => {
item.games.map((item) => {
console.log(item.gamename);
});
});
});
Try out this code, it will return only game names, you can change the join if don't need comma (,)
Output :
"cricket,football,videogames,volleyball"
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}];
console.log(JSON.stringify(arr.filter(e=>e.status=="success").map(e=>e.data.map(f=>f.games.map(g=>g.gamename)).join(",")).join(",")));

How to change the format of an array of objects based on ParentID

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),
},
};

json data question parents and children create

for(var d in response.data) {
var item = response.data[d];
console.log("test-1234=="+JSON.stringify(item));
}
If you take the data, it comes out as below.
test-1234=={"id":3,"isLeaf":"false","name":"bang","pid":0,"didabled":null}
test-1234=={"id":4,"isLeaf":"true","name":"test1","pid":3,"didabled":null}
test-1234=={"id":5,"isLeaf":"true","name":"test2","pid":3,"didabled":null}
test-1234=={"id":6,"isLeaf":"false","name":"test3","pid":0,"didabled":null}
I want to create a relationship between parents and children based on pid.
Like below.
Please give me a solution.
[
{
name: 'bang',
id: 3,
pid: 0,
dragDisabled: true,
children: [
{
name: 'test1',
id: 4,
isLeaf: true,
pid: 3
},
{
name: 'test2',
id: 5,
isLeaf: true,
pid: 3
}
]
},
{
name: 'test3',
id: 6,
isLeaf: true,
pid: 0
}
]
Try like this.
var arr = [
{ "id": 3, "isLeaf": "false", "name": "bang", "pid": 0, "didabled": null },
{ "id": 4, "isLeaf": "true", "name": "test1", "pid": 3, "didabled": null },
{ "id": 5, "isLeaf": "true", "name": "test2", "pid": 3, "didabled": null },
{ "id": 6, "isLeaf": "false", "name": "test3", "pid": 0, "didabled": null },
{ "id": 7, "isLeaf": "true", "name": "test\43", "pid": 4, "didabled": null }
]
var res = [];
for(var d in arr) {
var item = arr[d];
if(item.pid != 0){
findParentAndPush(item)
} else {
item.children = [];
res.push(item);
}
}
function findParentAndPush(item){
var filtered = arr.filter(data => data.id === item.pid);
item.children = [];
filtered[0].children.push(item)
}
console.log(res)

AngularJS: How to search and replace/add key in JSON object from other JSON?

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; }

Categories