I have an array of type:-
const arr = ['abc','def','ghi'];
but I want this array in the form of
const arr2 = [
{value: 'abc'},
{value: 'def'},
{value: 'ghi'},
];
I am not getting the idea to do it.
I have an array of type:-
const arr = ['abc','def','ghi'];
but I want this array in the form of
const arr2 = [
{value: 'abc'},
{value: 'def'},
{value: 'ghi'},
];
I am not getting the idea to do it.
use a map function to generate a new array with the type you want
for your reference, Array.prototype.map()
const arr=['abc','def','ghi'];
const arr2 = arr.map(item => ({value: item}));
console.log(arr2);
You can try using Array.prototype.map():
The
map()method creates a new array populated with the results of calling a provided function on every element in the calling array.
var arr = ['abc','def','ghi'];
arr = arr.map(value => ({value}));
console.log(arr);
You can use map to transform each item of an array to another value.
const arr = ['abc', 'def', 'ghi'];
const arr2 = arr.map(value => ({ value: value })
// const arr2 = [
// { value: "abc" },
// { value: "def" },
// { value: "ghi" }
// ];
If you want to be clever you can simplify this to
const arr2 = arr.map(value => ({ value })
See Array.prototype.map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map