You can use .map() for this. It passes the index into the callback.

myArray = myArray.map((str, index) => ({ value: str, id: index + 1 }));
Answer from Pointy on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-convert-string-to-array-of-objects-javascript
How to Convert String to Array of Objects JavaScript ? - GeeksforGeeks
August 5, 2025 - If the string is not in JSON format, you can manually split the string and construct objects to create the array, split() method splits the string into an array of individual records based on the semicolon delimiter.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-convert-string-of-objects-to-array-in-javascript
How to Convert String of Objects to Array in JavaScript ? - GeeksforGeeks
July 23, 2025 - The most common and straightforward way to convert a string of objects to an array of objects is by using JSON.parse() method. This method parses a JSON string into a JavaScript object or array.
Discussions

javascript - Convert array of strings into an array of objects - Stack Overflow
Since it was confusing for some ... valid JavaScript, instead of copy-pasted console outputs. ... Array#map applies the callback to each element in the array and returns a new array containing the return values of the callback. ... Sign up to request clarification or add additional context in comments. ... @Kinduser: The OP has an array of strings and wants an array of objects... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I convert an array of strings to an array of objects in JavaScript? - Stack Overflow
1 What is the best way to convert an array of strings into an array of objects? ... 0 How can I convert a string containing an array of javascript objects, into an array of javascript objects? More on stackoverflow.com
๐ŸŒ stackoverflow.com
node.js - How can I convert string to array of objects in JavaScript - Stack Overflow
This is the string Option 1|false|Option 2|false|Option 3|false|Option 4|true I want to convert it to array of objects like this ยท Is This Possible In javaScript Nodejs???? More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 2, 2020
jquery - Best way to convert string to array of object in javascript? - Stack Overflow
I want to convert below string to an array in javascript. {a:12, b:c, foo:bar} How do I convert this string into array of objects? Any cool idea? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-convert-array-to-object
Convert an Array to an Object using JavaScript | bobbyhadz
We used the string "key" + the index, however, you can adjust this as needed. ... Use the Array.map() method to iterate over the array. Return an array of a key and a value on each iteration.
Find elsewhere
Top answer
1 of 7
84

I think that the best way of doing this, as Douglas Crockford (one of the biggests gurus of JavaScript) suggests in here is using the JSON native parser, as it is not only faster than the eval(), it's also more secure.

Native JSON parser is already available in:

  • Firefox 3.5+
  • IE 8+
  • Opera 10.5+
  • Safari Safari 4.0.3+
  • Chrome (don't know which version)

And Crockford has made a safe fallback in javascript, called json2.js, which is an adaption of the eval() approach, with some security bits added and with the native JSON parsers API. You just need to include that file, remove its first line, and use the native JSON parser, and if it's not present json2 would do the work.

Here is an example:

var myJSONString = '{ "a": 1, "b": 2 }',
    myObject = JSON.parse(myJSONString);

Once parsed you'll get an object with attributes a and b, and as you may know, you can treat an object as a hash table or associative array in JavaScript, so you would be able to access the values like this:

myObject['a'];

If you just want a simple array and not an associative one you could do something like:

var myArray = [];
for(var i in myObject) {
    myArray.push(myObject[i]);
}

Lastly, although not necessary in plain JavaScript, the JSON spec requires double quoting the key of the members. So the navite parser won't work without it. If I were you I would add it, but if it is not possible use the var myObject = eval( "(" + myString + ")" ); approach.

2 of 7
10

Since your string is malformed JSON, a JSON parser can't parse it properly and even eval() will throw an error. It's also not an Array but a HashMap or simply an Object literal (malformed). If the Object literal will only contain number and string values (and no child objects/arrays) you can use the following code.

function malformedJSON2Array (tar) {
    var arr = [];
    tar = tar.replace(/^\{|\}$/g,'').split(',');
    for(var i=0,cur,pair;cur=tar[i];i++){
        arr[i] = {};
        pair = cur.split(':');
        arr[i][pair[0]] = /^\d*$/.test(pair[1]) ? +pair[1] : pair[1];
    }
    return arr;
}

malformedJSON2Array("{a:12, b:c, foo:bar}");
// result -> [{a:12},{b:'c'},{foo:'bar'}]

That code will turn your string into an Array of Objects (plural).

If however you actually wanted a HashMap (Associative Array) and NOT an array, use the following code:

function malformedJSON2Object(tar) {
    var obj = {};
    tar = tar.replace(/^\{|\}$/g,'').split(',');
    for(var i=0,cur,pair;cur=tar[i];i++){
        pair = cur.split(':');
        obj[pair[0]] = /^\d*$/.test(pair[1]) ? +pair[1] : pair[1];
    }
    return obj;
}

malformedJSON2Object("{a:12, b:c, foo:bar}");
// result -> {a:12,b:'c',foo:'bar'}

The above code will become a lot more complex when you start nesting objects and arrays. Basically you'd have to rewrite JSON.js and JSON2.js to support malformed JSON.

Also consider the following option, which is still bad I admit, but marginally better then sticking JSON inside an HTML tag's attribute.

<div id="DATA001">bla</div>
<!-- namespacing your data is even better! -->
<script>var DATA001 = {a:12,b:"c",foo:"bar"};</script>

I am assuming you omit quote marks in the string because you had put it inside an HTML tag's attribute and didn't want to escape quotes.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ convert-an-array-to-an-object-in-javascript
JavaScript - Convert an Array to an Object - GeeksforGeeks
July 11, 2025 - You can use it to convert an array to an object by iterating over the array and building an object with key-value pairs. ... const a = ['Hello', 'World', 'Welcome', 'To', 'JavaScript']; const obj = a.reduce((acc, current, index) => { acc[index] ...
๐ŸŒ
Make Community
community.make.com โ€บ questions
Transform an array of strings into an array of objects - Questions - Make Community
March 3, 2024 - Hello! Hopefully a simple one. I have an array of strings hitting my webhook. The data in question is leadSources: [ "One", "Two" ] I want to perform a simple transformation of this so I get an array of objects likโ€ฆ
๐ŸŒ
xjavascript
xjavascript.com โ€บ blog โ€บ best-way-to-convert-string-to-array-of-object-in-javascript
Best Ways to Convert a String to an Array of Objects in JavaScript: A Practical Guide โ€” xjavascript.com
If your string is a valid JSON array of objects, JSON.parse() is the simplest and most efficient solution. JSON (JavaScript Object Notation) is a standard data format, so APIs, databases, and files often return data in this structure.
๐ŸŒ
Stack Exchange
salesforce.stackexchange.com โ€บ questions โ€บ 398930 โ€บ convert-array-of-objects-to-an-object-in-javascript
map - Convert array of objects to an object in javascript - Salesforce Stack Exchange
March 12, 2023 - Why are we using a Map over a JS object : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#objects_vs._maps ยท Step 1 : Convert each object in the array to an array itself using the map method :
๐ŸŒ
YouTube
youtube.com โ€บ watch
Easiest Way to Convert an Array into Object | JavaScript - YouTube
#shorts Please like, share and subscribe if you found the video useful. Channel Link: https://www.youtube.com/c/RethinkingUI/videosCheckout the Playlists: ๐Ÿ‘‰
Published: October 16, 2022
๐ŸŒ
ServiceNow Community
servicenow.com โ€บ community โ€บ developer-forum โ€บ how-to-convert-string-to-an-array-object โ€บ td-p โ€บ 3228907
How to convert String to an Array Object - ServiceNow Community
April 7, 2025 - var value = g_form.getValue('PUT ... 1."},{"name":"Pay","id":"GUID2","description":"DESC 2"}]'; // Convert to JavaScript object (array) var parsedArray = JSON.parse(jsonString);...
๐ŸŒ
YouTube
youtube.com โ€บ all things javascript, llc
Converting a JavaScript String to an Array - YouTube
Effective manipulation of strings is important in JavaScript. At times you may need to work with individual characters. The most effective way of doing this ...
Published: September 5, 2018
Views: 17K
๐ŸŒ
Plain English
plainenglish.io โ€บ home โ€บ blog โ€บ javascript โ€บ 5 ways to convert string to array in javascript
5 Ways to Convert String to Array in JavaScript
November 23, 2020 - We can just wrap all those characters inside an array literal to create a new array from the string. let str = 'jscurious'; let arr = [...str]; console.log(arr); // ["j", "s", "c", "u", "r", "i", "o", "u", "s"] This method is used to copy the ...