Based on your updated question, I take it that you have this:
const start = ["Toronto, Ontario: 95", "Markham, Ontario: 71"];
and you want this:
result = [
{"Toronto, Ontario": 95},
{"Markham, Ontario": 71}
];
To do that, you need to split the number at the end of the string off, and then build objects with the two parts of the string. If you know there's only ever one :
, then:
const result = start.map(str => {
const [city, number] = str.split(":");
return {
[city]: +number
};
});
Live Example:
const start = ["Toronto, Ontario: 95", "Markham, Ontario: 71"];
const result = start.map(str => {
const [city, number] = str.split(":");
return {
[city]: +number
};
});
console.log(result);
That uses destructuring to capture the two parts from split
, then a computed property name to create a property with the city name, and +
to coerce the number to number from string. (That's just one of your number-to-string options, see this answer for more options.)