Guide on removing the parent JSON array while retaining the child JSON array

I have been searching for a solution to remove specific JSON array elements, but I have not found one yet. Here is the JSON data I am working with:

[
    {
        "KODE": [
            {
                "name": "kode",
                "value": [
                    {
                        "value": "DIAG001",
                        "RankNum": 1
                    },
                    {
                        "value": "DIAG002",
                        "RankNum": 1
                    },
                    {
                        "value": "DIAG003",
                        "RankNum": 1
                    },
                    {
                        "value": "DIAG004",
                        "RankNum": 1
                    }
                ]
            }
        ],
        "HEALING": [
            {
                "name": "Encountrance",
                "value": [
                    {
                        "Diagnosis_Code": "DIAG001",
                        "Name": "Subsequent"
                    },
                    {
                        "Diagnosis_Code": "DIAG002",
                        "Name": "Initial"
                    },
                    {
                        "Diagnosis_Code": "DIAG003",
                        "Name": "Initial"
                    },
                    {
                        "Diagnosis_Code": "DIAG004",
                        "Name": "Subsequent"
                    }
                ]
            }
        ]
    }
]

I need to remove the parent JSON array of KODE and HEALING so that my JSON looks like this:

[
    {
        "name": "kode",
        "value": [
            {
                "value": "DIAG001",
                "RankNum": 1
            },
            {
                "value": "DIAG002",
                "RankNum": 1
            },
            {
                "value": "DIAG003",
                "RankNum": 1
            },
            {
                "value": "DIAG004",
                "RankNum": 1
            }
        ]
    },
    {
        "name": "Encountrance",
        "value": [
            {
                "Diagnosis_Code": "DIAG001",
                "Name": "Subsequent"
            },
            {
                "Diagnosis_Code": "DIAG002",
                "Name": "Initial"
            },
            {
                "Diagnosis_Code": "DIAG003",
                "Name": "Initial"
            },
            {
                "Diagnosis_Code": "DIAG004",
                "Name": "Subsequent"
            }
        ]
    }
]   

Can anyone provide guidance on how to achieve this programmatically?

Answer №1

To streamline the array, consider utilizing the object values for concatenation.

var data = [{ KODE: [{ name: "kode", value: [{ value: "DIAG001", RankNum: 1 }, { value: "DIAG002", RankNum: 1 }, { value: "DIAG003", RankNum: 1 }, { value: "DIAG004", RankNum: 1 }] }], HEALING: [{ name: "Encountrance", value: [{ Diagnosis_Code: "DIAG001", Name: "Subsequent" }, { Diagnosis_Code: "DIAG002", Name: "Initial" }, { Diagnosis_Code: "DIAG003", Name: "Initial" }, { Diagnosis_Code: "DIAG004", Name: "Subsequent" }] }] }],
    result = data.reduce((r, o) => r.concat(...Object.values(o)), []);
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying an error message in a spreadsheet cell

Issue The problem arises when viewing the exported excel file where undefined is displayed. However, upon investigation, there are no empty array indexes causing this confusion. Solution Attempt const joinDate = new Date(obj.date); let snap = [ obj. ...

Ways to eliminate the identical element from an array

In my JavaScript code, I'm working with an array like this: ['html', 'css', 'perl', 'c', 'java', 'javascript'] I want to know how to remove the "perl" element from this array. The objectiv ...

Using jQuery to scroll within a table

I am currently working on a project that involves displaying specific data rows. To achieve this, I have decided to use a table. For a better understanding, you can check out a table demo here. The issue I am facing is that whenever I change the selected ...

ReactJS bug: Array rendering problem affected by recent changes

Why does ReactJS remove the first element instead of the middle element when using array.splice to remove an element from an array? This is my code. I am using Redux as well. const reducerNotesAndLogin = (state = initialState, action) => { var tableNo ...

Deciphering intricate JSON using Gson in Java - Focusing solely on nodes

I am working with a Json structure that contains information for "myId", "name", "address1", and "city". { "InformationResponse":{ "Id":"122212", "customerSessionId":"007", "Summary":{ "myId":1234567, ...

Enhancing Win8 apps with AppendTo/jquery-win8

Recently, I've been intrigued by the ToDoMVC samples and decided to try porting them into a Windows 8 JS app. I thought it would be as simple as copying and pasting the code while making sure to reference the necessary WinJS libraries. However, I soo ...

How can we troubleshoot webdriver cucumber when test steps are skipped due to passed arguments?

Greetings! I have a feature file outlined below in my cucumber webdriver test project: # language: en Feature: Simple Test, Int and Prod Origin vs non origin checks Simple cloud environment Checks @javascript @EnvCheck Scenario: Check Env TEST Orig ...

Insert comments into MySQL database using asynchronous JavaScript and JSON technology

Looking at the diagram, I have a scenario where two tables exist in my MySQL database and the goal is to enable the system to add and retrieve comments without the need to refresh the page. This functionality involves three PHP pages: 'DB.php', ...

The Protractor tool (node:9208) threw an UnhandledPromiseRejectionWarning due to an ElementNotVisibleError, indicating that the element is not interactable. Despite this

I am attempting to interact with an element using protractor but encountering the following error (node:9208) UnhandledPromiseRejectionWarning: ElementNotVisibleError: element not interactable (Session information: chrome=69.0.3497.92) (Driver informa ...

jquery animation does not reset after event occurs

My script is functioning well to animate my elements, but I am facing an issue where when the function is called again after a timer, the elements move to their correct positions but do not initiate a new animation. The goal of the function updateFlights( ...

Having trouble with my React private route, can't seem to get it to function properly

I'm fairly new to React, and I've set up a private route with an authToken passed through props from the state of App.js. The idea is that if the authToken is true, the private route should direct me to /HomePage, but whenever I log in with valid ...

Clicking on a table will toggle the checkboxes

I am facing an issue with this function that needs to work only after the page has finished loading. The problem arises due to a missing semicolon on the true line. Another requirement is that when the checkbox toggle-all is clicked as "checked", I want ...

How can you ensure a form is properly validated using javascript before utilizing ajax to submit it

I've been working on developing a website and I am in need of an attractive login/registration/forgot password form. My aim was to utilize 'ajax' to enhance the user experience, leading me to immerse myself in a steep learning curve for the ...

Pull the data from jQuery/JavaScript/AJAX and store it in the database using ASP.NET/C#

I am working on creating a form that includes textboxes and a five star rating feature. The goal is to save the data entered in the fields into a database upon submitting. While implementing the textboxes was straightforward, I am facing challenges with e ...

Is there a way to create a list of languages spoken using Angular?

I am in search of a solution to create a <select> that contains all the language names from around the world. The challenge is, I need this list to be available in multiple languages as well. Currently, I am working with Angular 8 and ngx-translate, ...

Retrieve the class using a text string and then obtain the subsequent string

I am searching for the first class containing the text "Routed:" and then I want to retrieve the following string. I attempted to utilize document.querySelector, but I'm uncertain if this is the correct approach. <td class="msvb2">Routed:</t ...

Ajax received a response from http 409 and is now parsing it

Hey there! I've been working on parsing out the message object using Ajax, and I'm having a bit of trouble getting a reference to messages.msg. It's strange because it displays perfectly fine in Postman, but for some reason, I can't see ...

Performing synchronous POST requests to manipulate data in a SQL database using AJAX

My goal is to send synchronous calls to a page that will handle the SQL insertion of words I am posting. However, due to the large number of chunks and the synchronous nature of SQL, I want each AJAX call to be processed one after another. for (chunk = 1; ...

The intersection observer fails to detect any new elements or entries that are appended to the page after it has

When I press the "add section" button to create a new section, the intersection observer does not seem to observe it. Even when I try to run the observer again after pressing the button, it still doesn't work. I suspect that I need to reassign the `se ...

How can I dynamically update a React/Next.js page as the server completes different stages of a complex action?

Currently, my aim is to develop a single-page application that relies on one major back-end process. This procedure is initiated by a singular string input. The various stages involved and the outcomes are as follows: It takes about 20ms to display/render ...