SCSS, преобразованный в массив, нужно правильно перебрать и создать вложенные массивы

Допустим есть такой массив: let arr = ['asad{','font-size:12px', 'qwe{', 'asd{','}','}','}'] на выходе нужно , чтоб получилось:

[
    [
      'asad{',
      'font-size:12px', 
      [
       'qwe{', 
       ['asd{','}'],
       '}'
      ],
     '}'
    ]
]

Ответы (1 шт):

Автор решения: vsemozhebuty

Так подойдёт?

const arr = ['asad{', 'font-size:12px', 'qwe{', 'asd{', '}', '}', '}'];

const result = fillLevel(arr, []);

console.log(JSON.stringify(result, null, '  '));

function fillLevel(source, destination) {
  while (source.length) {
    const chunk = source.shift();

    if (chunk.includes('{')) {
      destination.push(fillLevel(source, [chunk]))
    } else {
      destination.push(chunk);
      if (chunk.includes('}')) return destination;
    }
  }

  return destination;
}

→ Ссылка