0

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?

4
  • 1
    Paste also your code please, otherwise this can't be really usefull Commented May 9, 2019 at 11:12
  • 4
    Please provide the result you would like to get. Commented May 9, 2019 at 11:13
  • please provide an example of the expected result, and also your attempt at coding it so far. We like to help people here, but not write their whole code and algorithm for them. What have you tried? What is going wrong? You've told us you're "unable to" do it, but that isn't an error or bug which we can fix. Commented May 9, 2019 at 11:19
  • Please find the updated post Commented May 9, 2019 at 11:27

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

How can I get Type:'D' data only out of this with date?
All type "D" are 0 indexed you can write like this return {"date":item.date,"Details":item.Details[0]}
Am I missing something but doesn't this answer return the same input array? This is because the input array is in the format { date, Details } and the mapping function simply returns { date, Details }?
here you have "C" and "D". I wasted my time to solved that rigth ?
0

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 ?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.