I have a array as follows:
[
{
data: {
name: "Cloud",
size: "20mb",
type: "Folder",
DisplayStatus: 1,
},
children: [
{
data: {
id: 1,
name: "backup-1.zip",
size: "10mb",
type: "Zip",
isDisplayStatus: 1,
},
},
{
data: {
id: 2,
name: "backup-2.zip",
size: "10mb",
type: "Zip",
isDisplayStatus: 1,
},
},
],
},
{
data: {
name: "Desktop",
size: "150kb",
type: "Folder",
DisplayStatus: 0,
},
children: [
{
data: {
id: 3,
name: "note-meeting.txt",
size: "50kb",
type: "Text",
isDisplayStatus: 0,
},
},
{
data: {
id: 4,
name: "note-todo.txt",
size: "100kb",
type: "Text",
isDisplayStatus: 1,
},
},
],
},
];
I have a method in which I will receive two arguments. One is boolean value and one is id.
-
If boolean value is false, then I need to find the element in the array for the id which is coming as argument of method and set "isDisplayStatus" field to 0.
-
If boolean value is true, then I need to find the element in the array for the id which is coming as argument of method and set "isDisplayStatus" field to 1.
-
If for any children "isDisplayStatus" is 0, then "DisplayStatus" in parent should be set as 0. If all chilren "isDisplayStatus" is 0, then for parent "DisplayStatus" should be 0. If all children "isDisplayStatus" is 1, then for parent "DisplayStatus" should be 1.
My Try:
myMethod(flag, data) {
if (flag == true) {
this.isDisplayStatus = 1;
} else if (flag == false) {
this.isDisplayStatus = 0;
}
this.myArray = this.myArray.reduce(
(f, i) => [
...f,
{
...i,
children: i.children.map((child) => ({
...child,
data: {
...child.data,
...(child.data.id === data.id
? {
isDisplayStatus: this.isDisplayStatus,
}
: {}),
},
})),
},
],
[]
);
}
I am able to achieve 1 & 2 point but I am not able to achieve point 3. Please help
How can I do this?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/lxXsT3U7w
Comments
Post a Comment