Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an array to an object by splitting strings?

I have an array like below:

["gender-m", "age-20", "city-london", "lang-en", "support-home"]

I tried to generate a JSON object:

{"gender":"m", "age":"20", "city":"london", "lang":"en", "support":"home"}

One solution I can think of is using FOR loop to make it, but I am sure there are elegant solutions for this. Any suggestions, please help me.

like image 981
franco phong Avatar asked Oct 22 '19 15:10

franco phong


People also ask

How do you split an array into an object?

Splitting the Array Into Even Chunks Using slice() Method The easiest way to extract a chunk of an array, or rather, to slice it up, is the slice() method: slice(start, end) - Returns a part of the invoked array, between the start and end indices.

Can you use split on an array?

The split() method splits (divides) a string into two or more substrings depending on a splitter (or divider). The splitter can be a single character, another string, or a regular expression. After splitting the string into multiple substrings, the split() method puts them in an array and returns it.

What is string splitting?

String splitting is the process of breaking up a text string in a systematic way so that the individual parts of the text can be processed. For example, a timestamp could be broken up into hours, minutes, and seconds so that those values can be used in the numeric analysis.

How do I convert an array to an object in JavaScript?

We'll go over each of those methods in this article and provide some code examples for you to use on your website or application. Let's get started! 1. Object.assign () Object.assign () is the first method we'll cover for converting an array to an object. This method is used to copy values from one or more source objects to a new object.

How to convert array to object in AutoCAD?

Table of Contents 1 Object.assign () Object.assign () is the first method we'll cover for converting an array to an object. ... 2 Loop Over Array & Construct a New Object For the second method, we're going to loop over each item in an array and add each of its values as ... 3 Reduce ()

How to convert an object to an array[] of key-value pairs?

We can convert an Object {} to an Array [] of key-value pairs using methods discussed below: Method 1: In this method, we will use Object.keys () and map () to achieve this. Approach: By using Object.keys (), we are extracting keys from the Object then this key passed to map () function which maps the key and corresponding value as an array, ...

How to convert an array to an object in JSON?

For display it, JSON.stringify () method is used. | Convert Array to Object. Example-2: This example converts the array to object by creating a function which adds the array values one by one to the object. For display it, JSON.stringify () method is used.


1 Answers

You could take Object.fromEntries with the splitted key/value pairs.

var data = ["gender-m", "age-20", "city-london", "lang-en", "support-home"],
    result = Object.fromEntries(data.map(s => s.split('-')));

console.log(result);
like image 91
Nina Scholz Avatar answered Nov 05 '22 22:11

Nina Scholz