kinnu
kinnu

Reputation: 388

Unable to get array from array in Javascript

I have an API call in my React js component which is returning a JSON object. Below is the JSON response:

[
    {
        "date": "2019-05-03 ",
        "Details": [
            {
                "Type": "D",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            },
            {
                "Type": "C",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-06 ",
        "Details": [
            {
                "Type": "D",
                "total": "1",
                "success": "0",
                "failure": "1",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABWW",
                                "NAME": "GWTHBC",
                                "Number": "3340373"
                            }
                        ]
                    }
                ]
            },
            {
                "Type": "C",
                "total": "1",
                "success": "0",
                "failure": "1",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "GWTHBC",

                                "Number": "3340373"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-07 ",
        "Details": [
            {
                "Type": "D",
                "total": "11",
                "success": "8",
                "failure": "3",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 2,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "53345342"
                            },
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "5334534"
                            }
                        ]
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "ISMIWP",

                                "Number": "7020191"
                            }
                        ]
                    }
                ]
            },
            {
                "Type": "C",
                "total": "11",
                "success": "8",
                "failure": "3",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 2,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "53345342"
                            },
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "5334534"
                            }
                        ]
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "ISMIWP",

                                "Number": "7020191"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-04 ",
        "Details": [
            {
                "Type": "D",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            },
            {
                "Type": "C",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            }
        ]
    }
]

Now for every date I want to form an array depending on Type(i.e. 'C' or 'D') with all success, failure and Data array but I am unable to get through Details.

Basically,I want an array of data for each date as such:

[
    {
        "date": "2019-05-03 ",
        "Details": [
            {
                "Type": "D",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            },
            {
                "Type": "C",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            }
        ]
    }
]

Below is what I tried:

var final=[];
       for (const item of data){
  final.push(item.Details);
       }
       console.log(final);
      });

This is not returning the array with date.

I tried foreach loop but that does not seems to work. Any idea how can I achieve that?

Upvotes: 0

Views: 93

Answers (2)

Darshana Pathum
Darshana Pathum

Reputation: 669

var obj = [
    {
        "date": "2019-05-03 ",
        "Details": [
            {
                "Type": "D",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            },
            {
                "Type": "C",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-06 ",
        "Details": [
            {
                "Type": "D",
                "total": "1",
                "success": "0",
                "failure": "1",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABWW",
                                "NAME": "GWTHBC",
                                "Number": "3340373"
                            }
                        ]
                    }
                ]
            },
            {
                "Type": "C",
                "total": "1",
                "success": "0",
                "failure": "1",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "GWTHBC",

                                "Number": "3340373"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-07 ",
        "Details": [
            {
                "Type": "D",
                "total": "11",
                "success": "8",
                "failure": "3",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 2,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "53345342"
                            },
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "5334534"
                            }
                        ]
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "ISMIWP",

                                "Number": "7020191"
                            }
                        ]
                    }
                ]
            },
            {
                "Type": "C",
                "total": "11",
                "success": "8",
                "failure": "3",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 2,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "53345342"
                            },
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "5334534"
                            }
                        ]
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "ISMIWP",

                                "Number": "7020191"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-04 ",
        "Details": [
            {
                "Type": "D",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            },
            {
                "Type": "C",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            }
        ]
    }
]

var output = []


obj.forEach(elem => {

  elem.Details.forEach( det => {

      if(det.Type === "D")
          output.push( {"date": elem.date, "Details": det} )

  })

})

console.log(output);

This is what you need right ?

Upvotes: 0

Shiva
Shiva

Reputation: 572

let input=[
    {
        "date": "2019-05-03 ",
        "Details": [
            {
                "Type": "D",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            },
            {
                "Type": "C",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-06 ",
        "Details": [
            {
                "Type": "D",
                "total": "1",
                "success": "0",
                "failure": "1",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABWW",
                                "NAME": "GWTHBC",
                                "Number": "3340373"
                            }
                        ]
                    }
                ]
            },
            {
                "Type": "C",
                "total": "1",
                "success": "0",
                "failure": "1",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "GWTHBC",

                                "Number": "3340373"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-07 ",
        "Details": [
            {
                "Type": "D",
                "total": "11",
                "success": "8",
                "failure": "3",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 2,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "53345342"
                            },
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "5334534"
                            }
                        ]
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "ISMIWP",

                                "Number": "7020191"
                            }
                        ]
                    }
                ]
            },
            {
                "Type": "C",
                "total": "11",
                "success": "8",
                "failure": "3",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 2,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "53345342"
                            },
                            {
                                "Cd": "SABW",
                                "NAME": "OCYHEH",

                                "Number": "5334534"
                            }
                        ]
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 1,
                        "TransDetails": [
                            {
                                "Cd": "SABW",
                                "NAME": "ISMIWP",

                                "Number": "7020191"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "date": "2019-05-04 ",
        "Details": [
            {
                "Type": "D",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            },
            {
                "Type": "C",
                "total": "0",
                "success": "0",
                "failure": "0",
                "Data": [
                    {
                        "name": "FailurePoint1",
                        "count": 0,
                        "TransDetails": []
                    },
                    {
                        "name": "FailurePoint2",
                        "count": 0,
                        "TransDetails": []
                    }
                ]
            }
        ]
    }
]

let output =input.map((item)=>{
  return {"date":item.date,"Details":item.Details}
});
console.log(output)

Upvotes: 2

Related Questions