-1

I have string like

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

I want to make array like

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

Please help me

2

1 Answer 1

1

Sample solution:

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.