v1
- by lffg 2/5/202100
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
const NAMES_COUNT = 100;
const ITEMS_COUNT = 1000;

const NAMES = Array.from({ length: NAMES_COUNT }).map((_, i) => i + 1);
const ITEMS = Array.from({ length: ITEMS_COUNT }).map(() => ({
	nome: NAMES[Math.floor(Math.random() * NAMES.length)],
  preco: Math.floor(Math.random() * 500)
}));
delete caserun single casemove downdrag and drop case


ready



function ric(arr) {
  return arr.reduce((soma, cur) => {
    let nome = cur.nome;
    let repetido = soma.find(elem => elem.nome === nome);
    if (repetido) repetido.preco += cur.preco;
    else soma.push(cur);
    return soma;
  }, []);
}

ric(ITEMS);
delete caserun single casemove upmove downdrag and drop case


ready



function lui(arr) {
  const map = new Map();
  for (const obj of arr) {
    const key = obj.nome;
    if (!map.has(key)) {
      map.set(key, obj);
      continue;
    }
    map.get(key).preco += obj.preco;
  }
  return [...map.values()];
}

lui(ITEMS);
delete caserun single casemove upmove downdrag and drop case


ready



function cmt(arr) {
  const resObject = {};
  arr.forEach((elem) => {
    const valorAnterior = resObject[elem.nome] || 0;
    resObject[elem.nome] = valorAnterior + elem.preco;
  });
  return Object.keys(resObject).map((key) => {
    return { nome: key, preco: resObject[key] };
  });
}

cmt(ITEMS);
delete caserun single casemove upmove downdrag and drop case


ready



function aug(arr) {
  let novoLista = [];
  let m = new Map();
  for(let prod of arr){
    if (m.has(prod.nome)){
      novoLista[m.get(prod.nome)].preco += prod.preco;                          
    } else {
      m.set(prod.nome, novoLista.push({nome:prod.nome,preco:prod.preco}) - 1);  
    }
  }
  return novoLista;
}

aug(ITEMS);
delete caserun single casemove updrag and drop case


ready



function vin(arr) {
  const groups = arr.reduce((acc, current) => {
    if (!acc[current.nome]) {
      acc[current.nome] = { ...current };
      return acc;
    }

    acc[current.nome].preco = acc[current.nome].preco + current.preco;
    return acc;
  }, {});
  return Object.values(groups);
}

vin(ITEMS);
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN