changing an array into json format using TypeScript

Looking to convert an array into JSON using TypeScript. How can I achieve the desired result shown below?

 let array = ['element1', 'element2', 'element3']

result = [{"value": "element1"}, {"value": "element2"}, {"value": "element3"}]

Answer №1

Just to clarify, if you were to represent your array in JSON format, it would look like this:

['element1', 'element2', 'element3']

Should you wish to include a value field, you can manually insert it before converting the array into JSON.

See It in Action

The code snippet below demonstrates the output:

[{"value":"element1"},{"value":"element2"},{"value":"element3"}]

let array = ['element1', 'element2', 'element3']

let arrayWithValue = array.map(el => ({value: el}));

console.log(JSON.stringify(arrayWithValue));

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

Is it possible to obtain the Apache server status (mod_status) output in JSON or XML format?

Exploring Apache's "mod_status" Feature: One of the functionalities provided by Apache is the "mod_status", which allows users to monitor the current status of their server. When accessed, it presents a wealth of information similar to the sample pag ...

Tips for enabling autofocus in mat-select列表。

I am working on an angular project where I am using Angular Material and a mat-select element. In my form, the mat-select is the first element, and I want to set auto-focus on it when the page loads. However, I have been facing some difficulties achieving ...

Using a string as a key for an object's property

function createObject(argName, argProperties){ var object = {}; object[argName] = argProperties; } I am calling my function in the following manner. createObject('name', objectProperties); The issue I am encountering is w ...

a guide on utilizing EVE to retrieve multiple values under the same key

I have a MongoDB collection named col10 with an Eve app running on it. I am attempting to retrieve data where multiple values are selected from the same key. For example: http://127.0.0.1:4567/col10?where={"var0053":[1130,1113]} ## returns 0 objects I ha ...

Is it considered acceptable to perform roughly 100 queries on the Android SQLite database for the purpose of storing a single data item?

I have recently incorporated SQLite data storage into my Android translator app. The current situation is as follows: the app receives translations in JSON format from a server, then proceeds to parse it and build an object tree consisting of 50 to 100 obj ...

Create a unique custom array by utilizing the data retrieved from the WordPress database using the $wpdb->get_results

I am currently working on transforming the output of a table and using wp_send_json to send it as a JSON response. The data is encoded as expected, but I would like to make some changes to the keys, formatting, and order of the data. I am unsure about how ...

Node JS application facing compatibility issues with Typescript not working as intended

I've been diving into Typescript and recently followed a tutorial on how to integrate it with an express api app. However, I encountered the following error: D:\Development\Node\Ed\TypeScript\src\app.ts:5 const app: Appl ...

The JSON reviver function is still not returning anything even when explicitly using the return

function _quotedText(data, config) { var pathKeys=config.customKey; console.log(pathKeys); //prints os_platform var inAuth_data=(JSON.parse(JSON.parse(JSON.parse(data["event.fields.custom_fields.inauth_device_data"])), (key, value) =& ...

Struggling to translate JavaScript code into Typescript

Currently in the process of converting my JavaScript code to Typescript, and encountering an error while working on the routes page stating Binding element 'allowedRoles' implicitly has an 'any' type. ProtectedRoutes.tsx const Protecte ...

I am having trouble viewing the JSON data on my activity's RecyclerView, which has been stored on a web server and accessed via a URL

When I try to run my activity, I am encountering an issue where I cannot see any JSON data from the webserver in my RecyclerView. All I see is a progress bar followed by an empty activity. I have already verified my adapter, I have set the adapter in the ...

Comparing Arrays.Stream and Stream.of: A Closer Look

Why does the output differ when using Arrays.stream and Stream.of? Ideally, both should return the same value. Input: int numbers[] = {1, 2, 3, 4}; System.out.println(Arrays.stream(numbers).count()); Output: 4 Input: int numbers[] = {1, 2, 3, 4}; System ...

What could be causing Next.js to re-render the entire page unnecessarily?

As a newcomer to Next.js, I am trying to develop an app where the header/navbar remains fixed at all times. Essentially, when the user navigates to different pages, only the main content should update without refreshing the navbar. Below is the code I have ...

String Converted to Array of Key-Value Pairs

Looking to convert a key-value pair string into a functional array? Here's what I have so far: $Array = "'Type'=>'Honda', 'Color'=>'Red'"; $MyArray = array($Array); However, this code is not returning a ...

Implementing controller-to-view communication in your application

I would like to display a notification indicating success or failure after submitting the form. Within my code, I have a Partial View named _Appointment.cshtml that contains the form. To load this Partial View on my main view, Index, I use the following ...

Implementing optional default values in React props using conditional types

I have a dilemma with conditional props types When attempting to set a default value for my optional prop within the conditional type, it causes issues with the types export type ChatBase = { id: string; title: string; } type ChatCardProps = { title: ...

Unveiling the method of retrieving a targeted value from JWT in React

I'm struggling to retrieve a specific value from my JWT token in React. I am using the react-jwt library to decode the token, and when I log it, I receive this output: Object { userId: "850dff98-54fb-4059-9e95-e44f5c30be0f", iat: 1698866016 ...

Create a JSON array from a collection using Backbone.js

I have a collection called Platforms in my backbone framework. The structure of this Platforms collection is organized as follows: Platforms PlatformList models 0: Platform attributes id: 1 name: "some name" 1 ...

Is it possible to obtain Literal types for object keys dynamically in typescript?

I am looking to extract the type of object keys. Below is a generic function for objects with keys as strings: type GenericInput = { [key:string]: {value:string,type:HTMLInputTypeAttribute,placeholder:string,min?:number,max?:number,required?:boolean, err ...

Retrieve content from Wordpress blog including image preview

Can someone help me find a PHP script that can import posts from my WordPress blog and display them on another website, complete with a thumbnail of each blog post? Thank you in advance for your assistance! ...

Display a free Admob banner within an Ionic 3 application

I have integrated Admob's banner into my Ionic 3 app following the guidelines provided in the Ionic documentation at this link. Below is the code snippet I used for displaying the banner on the homepage: import { Component } from '@angular/core ...