Navigate back to the homepage

how to convert string to key value pair in javascript?

Infinitbility
JavaScript
June 26th, 2022 · 1 min read
how to convert string to key value pair in javascript?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To convert string to key value pair in javascript, use the split(":") and Object.fromEntries() method. the split(":") method help you to convert string to array and Object.fromEntries() method will create array to object.

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.

The Object.fromEntries() method transforms a list of key-value pairs into an object.

In the following example, we will take example string and convert it on object.

1const str = "key:value";
2
3const strObj = Object.fromEntries([str.split(":")]) // {key: 'value'}

Today, I’m going to show you How do i convert string to key value pair in javascript, as above mentioned here, I’m going to use split(":") and Object.fromEntries() function to convert string to key value pair.

Let’s start today’s tutorial how do you convert string to key value pair in javascript?

In this example, we will do

  1. Take an example of key value string of array
  2. Use split(":") and Object.fromEntries() methods in a loop
  3. Store converted value into variable and console it
1const arr = ["id:1","name:infinitbility","domain:infinitbility.com"];
2
3const arrOfObj = arr.map((element) => {return Object.fromEntries([element.split(":")])})
4
5console.log(arrOfObj) // [{id: '1'},{name: 'infinitbility'},{domain: 'infinitbility.com'}]

In the above exampls, we have taken example of key value pair string and converted into array of object, Let’s check the output.

JavaScript, convert string to key value pair in javascript example
JavaScript, convert string to key value pair in javascript example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to convert string to number in javascript?

how to convert string to number in javascript?

An example of convert string to number in javascript with step by step example.

June 26th, 2022 · 1 min read
how to convert string to object key in javascript?

how to convert string to object key in javascript?

An example of using dynamic string to create an object key in javascript with a step-by-step example.

June 26th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]