javascript 从字符串中提取多个系列数组

javascript Extract multiple series array from string

我有这样的字符串

var t = 'red: 5, purple: 7, fuchsia: 10, green: 8';

我想制作这样的数组

a = ['red', 'purple', 'fuchsia', 'green'];
b = [ 5, 7, 10, 8]

请帮帮我

示例解决方案:

const t = 'red: 5, purple: 7, fuchsia: 10, green: 8';

const tarr = t.split(', ');

const a = [];
const b = [];

for (const item of tarr) {
  const [k, v] = item.split(': ');
  a.push(k);
  b.push(~~v); // '~~' is a shortcut to convert string value to number
}

console.log(a, b);