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))
Related
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);
I have the following array of objects with nested elements in the children property. I need to get objects by their id if the id matches.
[
{
"id": 10,
"name": "Scenarios",
"value": null,
"children": [
{
"id": 12,
"name": "Scenario status",
"value": null,
"children": []
}
]
},
{
"id": 11,
"name": "Forecast source",
"value": null,
"children": []
},
{
"id": 16787217,
"name": "Item#Cust",
"value": null,
"children": [
{
"id": 16787230,
"name": "Customer",
"value": null,
"children": [
{
"id": 16787265,
"name": "Site",
"value": null,
"children": []
},
{
"id": 16787291,
"name": "Commercial Network",
"value": null,
"children": []
},
{
"id": 16787296,
"name": "Distribution Site",
"value": null,
"children": []
}
]
},
{
"id": 16787245,
"name": "Item#Site",
"value": null,
"children": [
{
"id": 16787266,
"name": "Family#Warehouse",
"value": null,
"children": []
}
]
},
{
"id": 16787254,
"name": "Item",
"value": null,
"children": [
{
"id": 16787260,
"name": "Family",
"value": null,
"children": [
{
"id": 16787264,
"name": "Product line",
"value": null,
"children": []
}
]
},
{
"id": 16787261,
"name": "Group 1",
"value": null,
"children": []
}
]
}
]
},
{
"id": 16787267,
"name": "Supplier",
"value": null,
"children": []
},
{
"id": 16787297,
"name": "SKU",
"value": null,
"children": []
}
]
If no match on root element is found, the function should lookup its children to see if there is a match, and the first children match should be pushed on the root level.
For exemple, I have a list of ids: [12, 16787217, 16787245, 16787266]
The function should return only objects where ids matches while keeping hierarchy if matching parent id have matching children id, so it should return this:
[
{
"id": 12,
"name": "Scenario status",
"value": null,
"children": []
},
{
"id": 16787217,
"name": "Item#Cust",
"value": null,
"children": [
{
"id": 16787245,
"name": "Item#Site",
"value": null,
"children": [
{
"id": 16787266,
"name": "Family#Warehouse",
"value": null,
"children": []
}
]
}
]
}
]
As for now, I can get only the first level elements if they are found with this function:
filterArray(array: Array<any>, ids: Array<number>) {
array = array.filter(el => ids.includes(el.id));
for (let i = 0; i < array.length; i++) {
this.filterArray(array[i].children, ids);
}
console.log(array);
}
Anyone have an idea on how to achieve this?
You could reduce the array and take the nodes with found id and children or only the children at the base level.
const
find = (r, { children = [], ...o }) => {
children = children.reduce(find, []);
if (ids.includes(o.id)) r.push({ ...o, children });
else if (children.length) r.push(...children);
return r;
},
data = [{ id: 10, name: "Scenarios", value: null, children: [{ id: 12, name: "Scenario status", value: null, children: [] }] }, { id: 11, name: "Forecast source", value: null, children: [] }, { id: 16787217, name: "Item#Cust", value: null, children: [{ id: 16787230, name: "Customer", value: null, children: [{ id: 16787265, name: "Site", value: null, children: [] }, { id: 16787291, name: "Commercial Network", value: null, children: [] }, { id: 16787296, name: "Distribution Site", value: null, children: [] }] }, { id: 16787245, name: "Item#Site", value: null, children: [{ id: 16787266, name: "Family#Warehouse", value: null, children: [] }] }, { id: 16787254, name: "Item", value: null, children: [{ id: 16787260, name: "Family", value: null, children: [{ id: 16787264, name: "Product line", value: null, children: [] }] }, { id: 16787261, name: "Group 1", value: null, children: [] }] }] }, { id: 16787267, name: "Supplier", value: null, children: [] }, { id: 16787297, name: "SKU", value: null, children: [] }],
ids = [12, 16787217, 16787245, 16787266],
result = data.reduce(find, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could also try this
let data = [
{
"id": 10,
"name": "Scenarios",
"value": null,
"children": [
{
"id": 12,
"name": "Scenario status",
"value": null,
"children": []
}
]
},
{
"id": 11,
"name": "Forecast source",
"value": null,
"children": []
},
{
"id": 16787217,
"name": "Item#Cust",
"value": null,
"children": [
{
"id": 16787230,
"name": "Customer",
"value": null,
"children": [
{
"id": 16787265,
"name": "Site",
"value": null,
"children": []
},
{
"id": 16787291,
"name": "Commercial Network",
"value": null,
"children": []
},
{
"id": 16787296,
"name": "Distribution Site",
"value": null,
"children": []
}
]
},
{
"id": 16787245,
"name": "Item#Site",
"value": null,
"children": [
{
"id": 16787266,
"name": "Family#Warehouse",
"value": null,
"children": []
}
]
},
{
"id": 16787254,
"name": "Item",
"value": null,
"children": [
{
"id": 16787260,
"name": "Family",
"value": null,
"children": [
{
"id": 16787264,
"name": "Product line",
"value": null,
"children": []
}
]
},
{
"id": 16787261,
"name": "Group 1",
"value": null,
"children": []
}
]
}
]
},
{
"id": 16787267,
"name": "Supplier",
"value": null,
"children": []
},
{
"id": 16787297,
"name": "SKU",
"value": null,
"children": []
}
]
let ids = [12, 16787217, 16787245, 16787266]
let filterArray = (mArray, ids) => {
let result = []
for (const x of mArray) {
if (ids.includes(x.id)) {
let element = {...x, children: []}
let childrenFounds = []
if (x.children.length) {
childrenFounds = filterArray(x.children, ids)
if (childrenFounds.length) element.children.push(...childrenFounds)
}else {
delete element.children
}
result.push(element)
} else if (x.children.length) {
let found = filterArray(x.children, ids)
if (found.length) result.push(...found)
}
}
return result
}
let res = filterArray(data, ids)
console.log('res', res)
Considering I have a nested array something like:
[
{
"uid": "j5Dc9Z",
"options": [
{
"name": 'Transportation',
"checked": true,
},
{
"name": 'Medication',
"checked": true,
}
]
},
{
"uid": "a5gdfS",
"options": [
{
"name": 'Food supply',
"checked": true,
},
]
},
{
"uid": "jHab6i",
"options": [
{
"name": "Package distri",
"checked": true,
},
{
"name": "maintainence",
"checked": false
}
]
},
{...}
]
This array is rendered as a part of dynamic checkboxes and checked values are handled on checkbox onChange function. I need to create a final separate array of the options that are only checked: true.
Something like:
[
{
"name": "Medication",
"checked": true,
},
{
"name": 'Food supply',
"checked": true,
},
{
"name": 'Transportation',
"checked": true,
}, and so on..
]
I did try following approach, but it confused on how to achieve this final array:
const category = filterOptions.filters.filter(ele => ele.options.some(option => option.checked === true)).map(item => item.options.filter(data => data.checked === true));
console.log(category);
But these returns result in the format:
0: (2) [{name: 'Transportation', checked: true}, {name: 'Medication', checked: true}]
1: [{name: 'Food Supply', checked: true}]
length: 2
__proto__: Array(0)
Please help out to resolve the same
You can flatten the using Array.flatMap(), and then filter out unchecked items:
const data = [{"uid":"j5Dc9Z","options":[{"name":"Transportation","checked":true},{"name":"Medication","checked":true}]},{"uid":"a5gdfS","options":[{"name":"Food supply","checked":true}]},{"uid":"jHab6i","options":[{"name":"Package distri","checked":true},{"name":"maintainence","checked":false}]}]
const result = data.flatMap(o => o.options)
.filter(o => o.checked)
console.log(result)
You could map filtered arrays of wanted objects.
const
array = [{ uid: "j5Dc9Z", options: [{ name: "Transportation", checked: true }, { name: "Medication", checked: true }] }, { uid: "a5gdfS", options: [{ name: "Food supply", checked: true }] }, { uid: "jHab6i", options: [{ name: "Package distri", checked: true }, { name: "maintainence", checked: false }] }],
result = array.flatMap(({ options }) => options.filter(({ checked }) => checked));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use forEach():
const arr = [
{
"uid": "j5Dc9Z",
"options": [
{
"name": 'Transportation',
"checked": true,
},
{
"name": 'Medication',
"checked": true,
}
]
},
{
"uid": "a5gdfS",
"options": [
{
"name": 'Food supply',
"checked": true,
},
]
},
{
"uid": "jHab6i",
"options": [
{
"name": "Package distri",
"checked": true,
},
{
"name": "maintainence",
"checked": false
}
]
}
]
let finalArr = [];
arr.forEach(item => {
item.options.forEach((option) => {
if (option.checked === true) {
finalArr.push({...option})
};
});
});
console.log(finalArr);
Using array reduce() and spread operator:
var data = [
{
"uid": "j5Dc9Z",
"options": [
{
"name": 'Transportation',
"checked": true,
},
{
"name": 'Medication',
"checked": true,
}
]
},
{
"uid": "a5gdfS",
"options": [
{
"name": 'Food supply',
"checked": true,
},
]
},
{
"uid": "jHab6i",
"options": [
{
"name": "Package distri",
"checked": true,
},
{
"name": "maintainence",
"checked": false
}
]
}
];
var result = data.reduce((a, e) => [...a, ...e.options.filter(e => e.checked == true)], []);
console.log(result);
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)
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; }