What is the best way to transform an array of strings into a JSON object?

When navigating back to a list page, I want to make sure that the filter criteria are still retained. My solution involves using cookies, which are set when the form filter is submitted. In the list page's mounted hook, I retrieve the specific cookie as a string and use the split method to obtain the following result:

[
 "filter1:value",
 "filter2:value"
]

This results in an array of strings. How can I convert this into JSON format so that I can manipulate and insert each value into the v-model of the form filter?

*This pertains to a web application utilizing VueJS.

Answer №1

Utilize the Object.fromEntries method to create an object from elements in your array by splitting them with a : separator:

let info = [
 "category:example",
 "status:active"
]

let output = Object.fromEntries(info.map(item => item.split(":")))

console.log(output)

Answer №2

To convert data to and from JSON format, you can utilize the functions JSON.stringify and JSON.parse.

JSON.stringify({data: ["value1", "value2"]})
'{"data":["value1","value2"]}'
JSON.parse('{"data":["value1","value2"]}')
{data: ["value1", "value2"]}

Answer №3

By utilizing the initial row, we transform an array into a string; following that, we proceed to convert the string into a JSON object, which can then be modified as needed.

var arrayToString = JSON.stringify(Object.assign({}, arr));  // converting array into a string
var stringToJsonObject = JSON.parse(arrayToString); 

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 verify serde: org.openx.data.jsonserde.jsonserde

I am facing an issue while trying to create a table on hive. My original data is in json format, so I have downloaded and built serde as well as added all the required jar files for it to function properly. However, I keep encountering the following error: ...

Looking for assistance in reducing the vertical spacing between divs within a grid layout

Currently, I am in the process of developing a fluid grid section to showcase events. To ensure responsiveness on varying screen resolutions, I have incorporated media queries that adjust the size of the div elements accordingly. When all the divs are unif ...

Encountering issues while trying to generate a GitHub repository through Postman due to JSON parsing errors

When attempting to create a repository in the oops-project organization, I am using the following URL: https://api.github.com/orgs/oops-project/repos I have chosen Basic Authentication (username and password) and I am including both the name and descript ...

Utilize dplyr and stringr to extract words from a given text

I am currently exploring methods to extract specific words from a text column within a dataset. My current approach involves using the following code: library(dplyr) library(stringr) Text = c("A little bird told me about the dog", "A pig in a poke", "As ...

Insert HTML code that is activated when the selection is modified

I have a simple dropdown menu in my HTML code. HTML <select name="breed" onchange="breedChanged()"> <?php while ($row = gdrcd_query($result, 'fetch')){ ?> <option value="<?php echo $row['id_breed']; ?& ...

Tips for embedding Javascript code within the window.write() function

I have a JavaScript function that opens a new window to display an image specified in the data variable. I want the new window to close when the user clicks anywhere on it. I've tried inserting some JavaScript code within window.write() but it doesn&a ...

Tips for transferring parameters between AJAX requests

Struggling to pass parameters between two different ajax calls, I've noticed that the params only exist within the ajax scope and not outside of it. Is there another way to achieve this without calling from one ajax success section to another ajax? He ...

Enhancing a popup with animated effects

I have been working on a popup that I want to add a subtle animation to. A fade effect seems like the perfect solution. Here is the code for the button: <a href="javascript:void(0)" onclick="document.getElementById('back_overlay').style.disp ...

Having trouble with the jQuery load function not functioning properly

I have encountered an issue with this code snippet while running it on XAMPP. The alert function is working fine, but the HTML content fails to load. I have included links to jQuery 3.2.1 for reference. index.html $(document).ready(function(){ $("#b ...

Looping through items using v-model value in v-for

My website features a small form that allows users to submit new photos and view previously submitted photos. Users begin by selecting an album for the photo and then uploading it. Currently, I am encountering an issue in retrieving photos based on the sel ...

Making sure Angular picks up on $scope changes

Currently, I am in the process of developing my inaugural AngularJS application and am faced with the challenge of a directive not updating its view when there are changes to the array received from the service. Below is the structure of my directive: an ...

Having trouble with sending a JSON post request in Flask?

I have a setup where I am utilizing React to send form data to a Flask backend in JSON format. Here is an example of the code: add_new_user(e){ e.preventDefault() var user_details = {} user_details['fname'] = this.state.first_name user_d ...

JavaScript may encounter undefined JSON data after receiving a response from the server

I am facing an issue with my PHP script that is supposed to receive a JSON array. Here is the code I am using: $(function() { $("img").click(function() { auswahl = $( this ).attr("id"); $.get('mail_filebrowser_add2.php?datenID=' + aus ...

Gson Library: transform JSON containing null array field into an empty array

Is there a way to instruct GSON to create an empty array instead of setting it to null when a JSON with an array field has a NULL value? Are there any specific properties or flags that can be used for this purpose? ...

Ensuring validation with Javascript upon submitting a form

Hey there, I'm looking to validate field values before submitting a form. Here's the code I have: <table width="600" border="0" align="left"> <tr> <script type="text/javascript"> function previewPrint() { var RegNumber = docum ...

Can someone guide me on how to use contract.on() in ethers.js to listen to events from a smart contract in a node.js application?

I've been working on a node.js application using ethers.js to listen to events emitted from the USDT contract Transfer function. However, when I run the script, it exits quickly without displaying the event logs as expected. I'm unsure of what st ...

The schema definition created through an online Schema-Generator tool cannot be used with the Load Table API in BigQuery

Any assistance in this matter would be greatly appreciated. I am currently facing an issue with inserting various JSON documents into BigQuery. To avoid manual schema generation, I have been using online tools for Json Schema Generation. However, the sche ...

What is the best way to calculate the total duration (hh:mm) of all TR elements using jQuery?

I currently have 3 input fields. The first input field contains the start time, the second input field contains the end time, and the third input field contains the duration between the start and end times in HH:mm format. My goal is to sum up all the dur ...

Leverage JSON as your configuration file for PowerShell commands

$Settings = (Get-Content -Raw -Path $settingsFile)| ConvertFrom-Json -Verbose Output: @{key1=value1; key2=value2} From the input file: { "key1": "value1", "key2": "value2" } I seem to be receiving a string instead of the expected result. What c ...

"Unlocking the Potential of Babylon.js and Three.js for Exporting Pur

Currently, I am trying to convert a babylon.js model in .babylon format to either .obj or .stl (or any other format compatible with Maya). After searching for a solution within babylon.js itself, I found that three.js has a "save as obj" function in its ed ...