Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Desafio 1
//dado o array de dictionary abaixo some o total dos valores
let dictionary = [{ '5': 50, '6': 60 }, { 'A': 10 }, { 'YEEZY': 30 }]
dictionary.reduce((acc, val) => acc.concat(Object.values(val)), []).reduce((acc, val) => acc + val, 0)
@halan

This comment has been minimized.

Copy link

@halan halan commented Mar 9, 2017

const extractValues = (values, obj) =>
  [...values, Object.values(obj)]

const sum = (a, b) => a + b

dictionary.reduce(extractValues, []).reduce(sum, 0)
@halan

This comment has been minimized.

Copy link

@halan halan commented Mar 9, 2017

const mapObject = (mapper, obj) =>
  obj.reduce( (values, obj) => [...values, mapper(obj) ], [])

const sum = (a, b) => a + b

mapObject(Object.values, dictionary).reduce(sum, 0)
@anabastos

This comment has been minimized.

Copy link
Owner Author

@anabastos anabastos commented Mar 9, 2017

@halan Otima solução! Valeu

@halan

This comment has been minimized.

Copy link

@halan halan commented Mar 9, 2017

dictionary.map( Object.values ).reduce(sum, 0)
@halan

This comment has been minimized.

Copy link

@halan halan commented Mar 9, 2017

const mapObject = (mapper, obj) =>
  obj.reduce( (values, obj) => [...values, mapper(obj) ], [])

const sum = (a, b) => a + b

const sumValues = (obj) =>
  mapObject(Object.values, obj).reduce(sum, 0)

dictionary.map(sumValues).reduce(sum, 0)
@Woodsphreaker

This comment has been minimized.

Copy link

@Woodsphreaker Woodsphreaker commented Mar 9, 2017

const dictionary = [{ '5': 50, '6': 60 }, { 'A': 10 }, { 'YEEZY': 30 }],
sum = (valores) => valores.reduce((a, b) => a + b),
valores = dictionary.map(valor => sum(Object.values(valor))).reduce((a,b) => a + b)

@theuves

This comment has been minimized.

Copy link

@theuves theuves commented Mar 17, 2017

Sem reduce e sem callbacks (mas com eval).

eval(JSON.stringify(dictionary).match(/\d+(?!")/g).join('+'));
@Woodsphreaker

This comment has been minimized.

Copy link

@Woodsphreaker Woodsphreaker commented Mar 18, 2017

const obj =  [{ '5': 50, '6': 60 }, { 'A': 10 }, { 'YEEZY': 30 }];
const arrNumbers = () => obj.map(_a => Object.values(_a));
const onlyNumbers = () => [].concat(...arrNumbers());
const result = () => onlyNumbers().reduce((_a, _b) => _a + _b, 0);
@beatorizu

This comment has been minimized.

Copy link

@beatorizu beatorizu commented Mar 18, 2017

function sum_items(list_items) {
  var sum = 0;
  list_items.map(
    function (item) {
      for (var key in item) {
        sum += item[key];
      }
    }
  );
  return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment