Простой подход состоит в том, чтобы перебирать новый массив каждый раз, когда вы хотите добавить новое значение, и проверять, существует ли ключ.Если оно увеличивает значение, если не добавляет его.
Вот одно решение с временным объектом.
// Initial array
const myarray = ["Mark:40", "John:20", "Sofia: 60", "Mark:30"]
// Temporary object used to calculate values
const desired = {};
for (let value of myarray) {
// Index of the first digit in the string
const separatorIndex = value.indexOf(':') + 1;
// Key is everything before the first digit including the :
let key = value.substr(0, separatorIndex);
// If the key isn't present in the object add it
if (desired[key] === undefined) {
desired[key] = 0;
}
// Add the value of the key to the temporary object
// this combines the values of equal keys
desired[key] += parseFloat(value.substr(separatorIndex));
}
const desiredArray = [];
// Create an array from all values
for (let key in desired) {
desiredArray.push(key + desired[key].toString());
}
console.log(desiredArray);