i have this array of strings :
let myArray : ["AA","BB" , "CC" ...]
I want to convert it to an array of objects:
myArray = [{"id":1 , "value": "AAA"},{"id":2 , "value": "BBB"},{"id":3 , "value": "CCC"}...]
I ve trie with "let for":
for (let obj of ListObj) {
let resObj = {};
resObj ['value'] = obj ;
equipment = resObj ;
}
And with map :
ListObj.map(obj => { 'value' = obj })
Suggestions ?
To convert an array to an object, use the reduce() method to iterate over the array, passing it an object as the initial value. On each iteration, assign a new key-value pair to the accumulated object and return the result. Copied!
To convert a string to a char array, we can use the toCharArray() function. Detailed Procedure: Get the string. Call the toCharArray() method and store the character array returned by it in a character array.
The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.
Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');
You can use .map()
for this. It passes the index into the callback.
myArray = myArray.map((str, index) => ({ value: str, id: index + 1 }));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With