To answer your question simply you can use:
const af=a=>a.join(',').split(',').map(e=>e.trim()); // 45 character
and use it like:
let res = af(['apple', 'apple, mango, orange', 'orange']);
console.log(res);
Or if you prefer the long version:
/**
* Define the function
*/
function formatArray(arr) {
let tmp: string[];
let result: string[] = [];
tmp = arr.join(',').split(',');
tmp.forEach((el, i) => {
let str = el.trim(); // this is to remove the extra spaces
result.push(str); // add the element to final array
});
return result;
}
And use it as:
// The orignal array
let arr = ['apple', 'apple, mango, orange', 'orange'];
arr = formatArray(arr);
console.log(arr);
Note that trimming part in the function, is an option you might not need this in your case but I figured out this is what you might want.
reactjsflag