Convert array of strings into an array of objects
Another approach - Array#reduce
.
var arr = ["124857202", "500255104", "78573M104"];var res = arr.reduce(function(s, a){ s.push({name: a}); return s; }, []) console.log(res);
JS : Convert Array of Strings to Array of Objects
You can use .map()
for this. It passes the index into the callback.
myArray = myArray.map((str, index) => ({ value: str, id: index + 1 }));
Convert array of string into array of object with different keys JS
If you only have one array containing one set of data there's no need to use map
. Destructure the array elements, and then just create a new object using those variables.
const arr = [ '612772d3d8b2b2000482fc3c', '15', '15', 'Basketball', '2021' ];
const [ id, amount, bet, sport, date ] = arr;
const obj = [ { id, amount, bet, sport, date } ];
console.log(obj);
How to convert an Array to Array of objects with same keys in Javascript?
You can do this using Array.map
, which allows you to specify a function that returns a new item to replace in the array.
arr.map(o => ({ name: o }))
Here's one without fancy arrow function shorthand, just in case you are confused.
arr.map(function(o) {
return {
name: o,
}
})
How to convert an Array to Array of Object in Javascript
Try the "map" function from the array:
const output = [ 'John', 'Jane' ].map(name => ({name}));console.log(output);
What is the best way to convert an array of strings into an array of objects in Angular 2?
You can use map to iterate over the original array and create new objects.
let types = ['Old', 'New', 'Template'];
let objects = types.map((value, index) => {
return {
id: index + 1,
name: value
};
})
You can check a working example here.
Function to convert array of strings into array of objects
Change the following line :
o.key = array[i]
//change
o[key] = array[i]
function arrayToObjects(array, key) {
const objectArray = []; // To return an array later.
this.key = key;
for (const element of array) {
const o = new Object();
o[key] = element;
objectArray.push(o);
}
return objectArray;
}
How to convert an array of type String to an array object in javascript
What you have here is a JSON string. You can parse it to get the object / array:
var array = JSON.parse(arrayString)
Edit: I see your JSON string has single quotes. You need to replace all of them with double quotes before parsing:
JSON.parse(arrayString.replace(/'/g, '"'))
Related Topics
How to Plug a JavaScript Engine with Ruby and Nokogiri
Simple Function to Sort an Array of Objects
Youtube Iframe Player API - Onstatechange Not Firing
How to Export Tables to Excel from a Webpage
iOS Uiwebview JavaScript - Insert Data -Receive Callbacks
Applying Classname/Onclick/Onchange Not Working on Custom React Component
Meteor: Calling an Asynchronous Function Inside a Meteor.Method and Returning the Result
Copy Output of a JavaScript Variable to the Clipboard
React Native: Require() with Dynamic String
"Document.Getelementbyclass Is Not a Function"
How to Return Multiple Lines Jsx in Another Return Statement in React
How Persistent Is Localstorage
How to Implement Inheritance in Js Revealing Prototype Pattern
Stop a Youtube Video with Jquery