changing array into an object type within a JSON object using JavaScript

Is it possible to efficiently convert the object shown below?

"site_permissions": {
      "70488413-6750-4f0b-8947-061a85c49e20": [
        "8e0bc18c-0387-49e2-99ea-a6359b60d54d",
        "a014a059-8d43-4337-830c-e2742627cf47",
        "4ec12e88-64eb-4b0d-bfc7-7a46f562cf93"
      ]
    }

to

"site_permissions": {
      "70488413-6750-4f0b-8947-061a85c49e20": {
        "8e0bc18c-0387-49e2-99ea-a6359b60d54d",
        "a014a059-8d43-4337-830c-e2742627cf47",
        "4ec12e88-64eb-4b0d-bfc7-7a46f562cf93"
      }
    }

If so, what is the optimal way to achieve this transformation?

Answer №1

If you are familiar with the necessary key and value pairs for an object, and you wish to convert it accordingly, follow this approach:

"site_permissions": {
      "70488413-6750-4f0b-8947-061a85c49e20": {
        "8e0bc18c-0387-49e2-99ea-a6359b60d54d": 
        "8e0bc18c-0387-49e2-99ea-a6359b60d54d",

        "a014a059-8d43-4337-830c-e2742627cf47":
        "a014a059-8d43-4337-830c-e2742627cf47",

        "4ec12e88-64eb-4b0d-bfc7-7a46f562cf93":
        "4ec12e88-64eb-4b0d-bfc7-7a46f562cf93"
      }
    }

Utilize the following code snippet to achieve this conversion:

const site = {
    "70488413-6750-4f0b-8947-061a85c49e20": [
      "8e0bc18c-0387-49e2-99ea-a6359b60d54d",
      "a014a059-8d43-4337-830c-e2742627cf47",
      "4ec12e88-64eb-4b0d-bfc7-7a46f562cf93"
    ]
  };
  let newObject = {};
  Object.keys(site).map((key) =>{
    site[key].map((value) => 
    {
       newObject[value] = value;
    }
    );
    site[key] = newObject;
    newObject = {};
  });

  console.log(site);

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

Steer clear of nesting subscribe functions when a forkjoin is present within

Below is my angular code: this.service.save(body).pipe( switchMap(resp => { return this.dialog.confirmation({ message: 'save object successfully!' }); }), filter(ok => ok), tap(() => { this.pro.status = re ...

Obtaining a randomly selected item from a PHP array

Hey there, please bear with me as I navigate through learning how to communicate in programming languages. Can anyone assist me in extracting an entire object from a PHP array that contains multiple key-value pairs? <?php $quotes = array(); $quote ...

Fluent-ffmpeg is unable to handle video processing or tasks in Nodejs

I am facing an issue with the following code: const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path; const ffmpeg = require("fluent-ffmpeg"); ffmpeg.setFfmpegPath(ffmpegPath); app.put("/upload-content", async (req, res) ...

What is the best way to use a Handlebars file on multiple routes?

I have been working on extracting articles from a news website by scraping them successfully. The data is being displayed properly on the front-end console log, but I am facing an issue with rendering it onto the page using a button - it only appears when ...

Challenges with flex in AngularJS's Material Design layout

Having some trouble with setting the flex properties on my layout. For reference, here is a plunker link: http://embed.plnkr.co/0SrUp25FvT2PAsJDEwF3/preview <md-content flex layout="row" layout-align="center"> <div layout="column"> ...

Executing 'npm run bundle' with Webpack results in an ERR! with code ELIFECYCLE

As someone new to using Webpack with AngularJS apps, I am eager to learn but facing some challenges. Following the guide by Ken Howard has been helpful, but I encounter an error when attempting to run the bundle. Article by Ken Howard that I've been ...

Ways to retrieve the mapState property within a method

Is there a way to access the count property within the method while working with vuex? Take a look at my code provided below: Screenshot of Code: https://i.stack.imgur.com/xNUHM.png Error Message [Vue warn]: Computed property "count" was assigned to bu ...

Utilizing JSON Arrays to extract data and create three distinct arrays

I am currently working on extracting three arrays from the JSON Data retrieved from my restaurant. These arrays will represent categories such as Entre, Main, and Dessert, and will be displayed in a tableView Object. Below is the code snippet I am using to ...

Utilize JSON data loading instead of directly embedding it onto the page for improved website

I've integrated Mention.js into my website, allowing a dropdown list of usernames to appear when "@" is typed in a designated textarea. <textarea id="full"></textarea> While it's functioning well, the examples provided only show how ...

Angular directive ng-template serves as a component type

In the code snippet below, <!DOCTYPE html> <html> <head> ..... </head> <body ng-app="app"> <script type="text/ng-template" id="my-info-msg.html"> <s ...

Meteor: dynamic approach to assigning array values in Spacebars

It's hard to put this into words... I have a collection with an array, and as I iterate through it, I've been setting colors.[0].imageLink without changing the [0]. Now, I want this to be dynamic based on the value of a function (in this case, th ...

The strange interaction between Three.js and web workers

I've been experiencing some unusual behavior with web workers in a three.js application. Initially, everything looks fine when loading a new page. However, after about 20 page reloads, and only when web workers are running, this strange phenomenon oc ...

When access to Ajax .responseText in an alert it can be displayed, however it cannot be stored in a variable or

var response_var=""; // Added for debugging purposes ajax.onreadystatechange = function() { if (ajax.readyState == 4 & ajax.status == 200) { response_var = ajax.responseText; alert(ajax.responseText); // This alerts properly (some text ...

Connect Vue-switch value to a data property

Utilizing the vue-switch component to toggle on and off, I needed to determine its current state. Following guidance from the documentation at https://www.npmjs.com/package/vue-switch, I implemented :value.sync="toggle" in the switch component along with a ...

Capture input before onChange and ideally only accept numerical values

Preparing for inexperienced users, I have a table of input fields. My goal is to enhance user experience and make the form as user-friendly as possible. I have developed code that highlights the table row immediately after an input field has changed. Howe ...

Assign value to an element in an array using the value from another element

Is it possible in Javascript to set the value of one array element based on another without changing both elements? Specifically, how can I only modify arr[1] while leaving other elements unchanged? arr[0] = {i: 0}; arr[1] = arr[0]; arr[1]['summ&apos ...

It appears that the Office.EventType.ItemChanged event is not triggering as expected

Currently, I am exploring a simple console.log print test to see how it behaves when I switch from one email to another within Outlook. To guide me through this process, I have been referring to the instructions provided by Microsoft over at this link. In ...

Start the Express server by utilizing Grunt

Can anyone assist me with adding a task to my Gruntfile that will start my Express server instead of the default one? I attempted to create a task and use require("server.js"), but it doesn't seem to be working properly. When I run "grunt mytask", the ...

Leveraging the power of AJAX/JQuery to send form data to a PHP script

I've compiled this based on information from other sources on this platform and a variety of tutorials. I'm puzzled as to why it's not functioning, and I've had no success with alternative methods. Below are excerpts from the HTML of m ...

Cutting an in-memory Base64 PNG using ONLY JavaScript on the client side without relying on canvas

Scenario: Working with JavaScript in a SDK environment, either on node.js or a browser. Situation: I have a base64 string containing a PNG image encoded using base64 (extracted from selenium webdriver's takeScreenshot function). Inquiry: How can I c ...