A guide on extracting and transforming a nested Object into a new object containing essential information

Here is a nested Object that I have. It needs to be read and converted.

{
  "_id": "config",
  "_rev": "11-57a",
  "config": {
    "A": {
      "a1": {
        "required": true,
        "editable": true
      },
      "a2": {
        "required": true,
        "editable": true
      },
      "a3": {
        "required": false,
        "editable": false
      },
      "a4": {
        "required": false,
        "editable": true
      },
      "a5": {
        "required": true,
        "editable": true
      },
      "a6": {
        "required": false,
        "editable": true
      }
    },
    "B": {
      "b1": "b22",
      "b2": "b23"
    },
    "C": {
      "c1": "c22",
      "c2": "c33"
    }
  }
}

The data above needs to be converted into the following format:

{
"_id":"config",
"config":{
"A":{
"a1":{"required":true},
"a2":{"required":true},
"a3":{"required":false},
"a4":{"required":false},
"a5":{"required":true},
"a6":{"required":false}
}
}
}  

What's the best way to achieve this conversion in JavaScript?

Answer №1

It might be helpful to check out https://lodash.com/docs#get for a more efficient solution. Otherwise, you'll need to iterate through each nested level of your object, which may not be as visually appealing.

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

Unable to access HTML elements on an ASP .NET webpage through JavaScript

I've been trying to access the value of a textbox control with the runat="server" attribute, but I'm finding myself stuck searching for a solution. Each time I attempt to retrieve the value, it returns as null. Here is the javascript file: func ...

Is it possible to reload the webpage just one time after the form is submitted?

Can anyone suggest how I can refresh the webpage using PHP after submitting a form? I've been searching for a solution but haven't found one yet. Your assistance would be greatly appreciated. ...

Can you explain the functionality of this code in terms of conditional statements without the use of "if" or "else if" statements?

Upon encountering a nested logic problem that required solving, I stumbled upon an unconventional solution online which caught my eye. The code structure seemed unfamiliar to me and it intrigued me how the use of "if" and "else if" statements was differe ...

What causes Express Async Errors to produce unexpected outcomes?

I stumbled upon a fantastic npm package called Express Async Errors that is highly recommended in the documentation. However, when I try to implement it, my server crashes. Here is my Route handler code: Controller const { Genre } = require("../models"); ...

I am looking to consolidate my array of objects into a single object with distinct keys

Hey there! I'm looking to showcase the expenses for each category throughout the months of the year. Here's an example: {Month: "January", Food: 610, foodColor: "#063951", Others: 121, othersColor: "#C13018", …} Fo ...

Issue with popup closing even when back to top button is clicked

Whenever I click on a specific div, a popup appears. It's designed to close when I click anywhere outside of the popup. However, an issue arises when I click on elements like the 'Back to top button' - the popup closes even though I want it ...

Getting the badge value of a button in Angular JS is a simple process that involves accessing

How can I retrieve the badge value of a Button? This badge represents data-text-as-pseudo-element and I am new to Angular. Can someone guide me on how to achieve this? <style type="text/css">[data-text-as-pseudo-element]::before { content: ...

Using jQuery checkboxes with ajax for data submission and storage in PHP without the need for insertion

Seeking guidance on how to properly serialize elements value instead of pushing it, as I am encountering an issue where each value is being inserted into the database 9 times. Any assistance in figuring out this problem would be highly appreciated. The HT ...

How to achieve padding of strings in JavaScript or jQuery

Does anyone have information on a similar function in jQuery or JavaScript that mimics the prototype toPaddedString method? ...

Retrieving PHP form values using JQuery

How can I transfer a form input value from a PHP file to a JavaScript file? Here is the code I am currently using: <html> <head> <meta charset = "UTF-8"> <title> Search Customer by Field </title> <s ...

Resolving the Issue of Premature Header Output in NodeAPI Using Angular

I encountered an error in my Node API while simply logging the request to the console: router.get('/booksByISBN', checkRole, async (req, res) => { console.log(req.params) return res.sendStatus(200); }); node:internal/e ...

`the issue of $scope object not being passed correctly to ng-if and ng-class ternary conditions

**app.js:** $scope.servers = [ {name:'SQL Server', status:"up"}, {name:'Web Server', status:"down"}, {name:'Index Server', status:"down"} ]; **index.html:** <table> ...

Storing Angular views and ASP.NET MVC Views together within a single folder

Working on a project that involves Angular and ASP.NET MVC, I have decided to keep some Angular partials and MVC partials in the same folder for better organization. For example: /MyApp/Widgets/Image-config.html /MyApp/Widgets/Image-designer.html /MyApp/ ...

The error alert must be displayed directly beneath the specific box

When error messages occur in this code, they are displayed through an alert box. However, I would prefer to have the error message appear below the specific text box instead. This means that when I click on the submit button and a field is left empty, the ...

An issue occurred with the session being undefined in Node.js Express4

I'm encountering an issue where my session is undefined in new layers even after setting the value within an "if" statement. /*******************************************/ /**/ var express = require('express'), /**/ cookieParse ...

Optimal method for presenting informative content at the top of a webpage: Harnessing the power of JavaScript

Could you check out the image linked above? There appears to be a css animation on the bright yellow arrow. Issue: I am working on a page where I need to guide users' attention (possibly with a yellow arrow) and have it disappear automatically ...

Optimizing Animation Effects: Tips for Improving jQuery and CSS Transitions Performance

Wouldn't it be cool to have a magic line that follows your mouse as you navigate through the header menu? Take a look at this example: It works seamlessly and smoothly. I tried implementing a similar jQuery script myself, but it's not as smoot ...

What methods do publications use to manage HTML5 banner advertisements?

We are working on creating animated ads with 4 distinct frames for online magazines. The magazines have strict size limits - one is 40k and the other is 50k. However, when I made an animated GIF in Photoshop under the size limit, the image quality suffered ...

Determine the exact location of where the mouse was clicked within the

I've been attempting to determine the position of a click when a user clicks anywhere on the window. I came across this code in various tutorials, but unfortunately, it doesn't seem to be functioning as expected. (function( $ ) { $( document ...

Removing children from collection using JQuery selector based on class attribute

I am facing a bit of a challenge with a JavaScript plugin that exports HTML tables into Excel. I am not an expert in JavaScript, so I hope someone can help me out. The issue lies in the fact that the plugin excludes table rows based on a certain class, but ...