How to convert array of strings in Typescript

Paul

I want to convert string of arrays to an object.

input:

const test = [
  ' data1: 123',
  ' data2: 3524',
  ' data3: 5142',
  ' data4: 84521'
]

output:

{
 data1: 123,
 data2: 3524,
 data3:5142,
 data4: 84521,
} 
Behemoth

Use Object.fromEntries() and map the array to a fitting format. For instance with used Array.prototype.split().

const test = [
  'data1: 123',
  'data2: 3524',
  'data3: 5142',
  'data4: 84521'
];

const result = Object.fromEntries(test.map((e) => e.split(": ")))

console.log(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert an array of strings to an array of floats in numpy?

How to convert an array of bytes to an array of strings in Go?

How do I convert a Go array of strings to a C array of strings?

How to convert an array of custom objects to an array of strings?

How to convert a list of strings into a numeric numpy array?

How to define an array of strings in TypeScript interface?

How to convert SpecFlow table to array of strings

Convert array of strings to TypeScript type

How to convert array of strings to typescript types?

How to convert an array of strings to float in Javascript

How to limit the keys of an object to the strings of an array in Typescript?

How to convert typescript types of strings to array of strings?

How to convert array of strings into array of struct with conditions

How to convert an array of strings into values (symbols)?

How to convert array of strings into JSON object?

How to build a Typescript type from an array of strings?

How to convert this array of strings to array object

StringBuilder to Array of Strings how to convert

How to convert array of hash tables to array of strings

In Typescript, how do I convert a string of strings as an array of strings?

convert array of strings to object keys in typescript

How to convert array of values into strings in laravel?

how to convert an array of strings to array of numbers?

How to convert JSON to array of strings

How to convert list of enums into array of strings?

How to convert an array of strings into a number?

How to type N array of strings with TypeScript?

How to index interfaces with an array of strings in typescript

how to convert a string array to a boolean array in typescript