Convert Table data in a array to multiple objects in array

46 Views Asked by At

I have an array

data = [

    0 3x abcd aezakmi 900 
    1 2x defg panjer 800
    2 1x hijk  flyhigh 750

]

I want to convert it to this

data = [
 [`{0}{3x}{abcd}{aezakmi}{900}`]
 [`{1}{2x}{defg}{panjer}{900}`]
 [`{2}{1x}{jijk}{flyhigh}{700}`]
]

so that I can map them easily in the table.

1

There are 1 best solutions below

1
On

I changed array, because he had syntax error. Maybe this code is not ideal, but he work.

data = [
  '0 3x abcd aezakmi 900',
  '1 2x defg panjer 800',
  '2 1x hijk flyhigh 750',
];

const separate = (data) => {
  const arr = [];
  data.forEach((el) => {
    const obj = {};
    let i = 0;
    el.split(' ').forEach((el2) => {
      i += 1
      obj[i] = el2;
    })
    arr.push(`{${obj[1]}}{${obj[2]}}{${obj[3]}}{${obj[4]}}{${obj[5]}}`)
  });
  return arr;
};

console.log(separate(data));