Skip to content Skip to sidebar Skip to footer

Split And Fuse Words

Actually, I have this piece of code that takes the Words inside the Obj and it separates creating a new row for each column (this actually works perfectly), and it's exactly what I

Solution 1:

One possibility would be to create a regular expression to match comma-separated words (no quotes ' allowed), OR match a starting ' quote up until its matching ending ':

const Obj = {
    "0":"Masaccio, Sandro Botticelli, 'Leonardo, da, Vinci'",
    "1":"Random, Thing, Uploaded",
    "2":"String, Second String, Third string",
    "3":"Chef, Police, Cat",
    "4":"Legen, Jerry, Jack",
};
const result = Object.values(Obj).reduce((a, str) => {
  const items = str.match(/\w[\w\s]+(?=, |$)|'[^']+'/g);
  items.forEach((item, i) => {
    if (!a[i]) a[i] = [];
    a[i].push(item);
  });
  return a;
}, []);
console.log(result);

Post a Comment for "Split And Fuse Words"