1
let receipts_array =["Anna 0.4","Peter 0.25","Anna 0.5","Peter 0.5","Peter 0.33"]; // 

how can I split this array so I can have for example the first index to be just Anna and second 0.4, because i need to sum the numbers and then see who won output is just Peter

5 Answers5

1

You could use an object for summing the values of the names and reduce the keys by checking the values.

array.forEach(s => {
    var [k, v] = s.split(' ');
    count[k] = (count[k] || 0) + +v;
});

It means take every item of array as s, split that string by space and use a destructuring assignment of the array into two items with the name k and v as key and value.

Then use k as key of the object count, get this value and if not given take zero as a default value. Then add the value by taking an unary plus + for converting the string to a number.

Later assign the sum to the property k of count.

var array = ["Anna 0.4", "Peter 0.25", "Anna 0.5", "Peter 0.5", "Peter 0.33"],
    count = Object.create(null);
    
array.forEach(s => {
    var [k, v] = s.split(' ');
    count[k] = (count[k] || 0) + +v;
});

console.log(Object.keys(count).reduce((a, b) => count[a] > count[b] ? a : b));
console.log(count);

For expected same values, you could return an array with the winner names.

var array = ["Anna 0.4", "Peter 0.25", "Anna 0.5", "Peter 0.5", "Peter 0.33", "Foo 1.08"],
    count = Object.create(null),
    winner;
    
array.forEach(s => {
    var [k, v] = s.split(' ');
    count[k] = (count[k] || 0) + +v;
});

winner = Object
    .keys(count)
    .reduce((r, k) => {
        if (!r || count[k] > count[r[0]]) {
            return [k];
        }
        if (count[k] === count[r[0]]) {
            r.push(k);
        }
        return r;
    }, undefined);

console.log(winner);
console.log(count);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Hello Nina thank you for your help, about the first answer what this mean: array.forEach(s => { var [k, v] = s.split(' '); count[k] = (count[k] || 0) + +v; in plain english –  Mar 22 '18 at 16:42
0

Assuming the posted data sample, you can use the function split and function reduce.

let receipts_array = ["Anna 0.4", "Peter 0.25", "Anna 0.5", "Peter 0.5", "Peter 0.33"];
var result = receipts_array.reduce((a, c) => {
  var [name, number] = c.split(/\s+/);
  a[name.trim()] = (a[name.trim()] || 0) + +number.trim();
  return a;
}, {});

var winner = Object.keys(result).sort((a, b) => result[a] - result[b]).pop();

console.log(result);
console.log(winner);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75
0

You can map your array to another array with the itens splitted, like so:

const resulting_array = receipts_array.map(x => ({name: x.split(' ')[0], points: +x.split(' ')[1]}))

Then you can sort your resulting array and get the first place.

const first_place = resulting_array.sort((a, b) => a.points < b.points ? 1 : -1)[0]

this way you'll have the object with the person with the highest pontuation.

console.log(first_place.name)

I'd recommend you to store the info in an array of objects like the one gotten from the map function thoug.

Victor Ivens
  • 2,221
  • 2
  • 22
  • 32
0

I used Array.prototype.forEach to traverse array then split all element by space using split and then create one object that will hold value total score for different players and based on that score choose the winner.

var receipts_array =["Anna 0.4","Peter 0.25","Anna 0.5","Peter 0.5","Peter 0.33"];
var x=[],max=-10000,winner;

receipts_array.forEach(function(e){
  var y=e.split(" ");   //split the value
  if(!x[y[0]]){         //check if object with key exist or not
      x[y[0]]=parseFloat(0); //if object not exist create one
  }
  
  x[y[0]]+=parseFloat(y[1]);  // add score of player
  
  if(x[y[0]]>max){    //compare for max score
    max=x[y[0]];
    winner=y[0];
  }
});

console.log(winner);
yajiv
  • 2,901
  • 2
  • 15
  • 25
0

You can create a dictionary whose keys are the names.

let receipts_array = ["Anna 0.4", "Peter 0.25", "Anna 0.5", "Peter 0.5", "Peter 0.33"];
var result = receipts_array.reduce((obj, c) => {
  var [name, number] = c.split(/\s+/);
  obj[name] = (obj[name] || 0) + parseFloat(number);
  return obj;
},{});

console.log(result);
var winner = Object.keys(result).reduce(function(a, b){ return result[a] > result[b] ? a : b });
console.log('The winner is ' + winner)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128