I have an array of several strings like:
const stringsArray=["abc", "def", "ghi"];
I want to convert it into objects like:
const stringsObjects=[
{ label: "abc", value: "abc"},
{ label: "def", value: "def"},
{ label: "ghi", value: "ghi"},
{ label: "Others", value: "Others"}
];
I need to add one more object to the last with values as "others" as above. I tried using map and forEach methods but no luck. Can anyone guide me here using javascript here?
I tried using map and forEach methods but no luck.
const obj2= array1.forEach(element => {label: "element", value: "element"});
const selectOptions = industryKeywords.map(keyword => {return({
value: keyword,
label: keyword
},
{
value: "Others",
label: "Others"
}
)})
Both these ways gave me errors. I expect the below result:
const stringsObjects=[
{ label: "abc", value: "abc"},
{ label: "def", value: "def"},
{ label: "ghi", value: "ghi"},
{ label: "Others", value: "Others"}
];
forEach()doesn't natively return anything, so you'd have to explicitly push to your array. 2.){label: "element", value: "element"}:"element"is a string. You would need{label: element, value: element}. 3.)but no luckisn't specific. What actually happened? An error? An unexpected result?