Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Compare objects within an array and create a new array without duplicates

I've an array of objects:

[
  { name: "John", age: "34" },
  { name: "Ace", age: "14" },
  { name: "John", age: "45" },
  { name: "Harry", age: "11" },
]

I want to compare the objects within an array by name. If the duplicate name exists, I should compare the age and only keep the higher age object.

The expected output should be:

[
  { name: "Ace", age: "14" },
  { name: "John", age: "45" },
  { name: "Harry", age: "11" },
]

I am new to javascript/typescript and couldn't find any optimal solution for this problem. I hope, I was able to explain my problem clearly.

Thanks.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Maybe something like that

const obj = [{ name: "John", age: "34" }, { name: "Ace", age: "14" }, { name: "John", age: "45" }, { name: "Harry", age: "11" }];
const objCopy = JSON.parse(JSON.stringify(obj))

const res = objCopy.reduce((acc, obj) => {
  const personExist = acc.find(({ name }) => name === obj.name);
  if (personExist) {
    if (parseInt(obj.age, 10) > parseInt(personExist.age, 10)) {
        personExist.age = obj.age;
    }
  } else {
    acc.push(obj);
  }
  return acc;
}, []);

console.log({ res });
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

The next provided approach uses reduce and creates in a first step just an index/map of items of highest age which are each unique by name. Thus one could use the temporary state of the programmatically built result as lookup for already existing named items.

Within a second step one would retrieve the array of unique named items of highest age by passing such an index to Object.values.

function collectHighestAgeItemOfSameName(result, item) {
  const { name, age } = item;
  if (
    !(name in result) ||
    Number(result[name].age) < Number(age)
  ) {
    result[name] = item;
  }
  return result;
}

const sampleData = [{
  name: "John",
  age: "34"
}, {
  name: "Ace",
  age: "14"
}, {
  name: "Harry",
  age: "9"
}, {
  name: "John",
  age: "45"
}, {
  name: "Harry",
  age: "11"
}, {
  name: "Ace",
  age: "13"
}];

console.log(
  'reduced index of unique person items of highest age ...',
  sampleData
    .reduce(collectHighestAgeItemOfSameName, {})
)
console.log(
  'array of unique person items of highest age ...',
  Object
    .values(
      sampleData
        .reduce(collectHighestAgeItemOfSameName, {})
    )
)
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

try this

var objArr=...your json object;
var maxValueGroup = "name";
var maxValueName = "age";
console.log(JSON.stringify(newArr(objArr,maxValueGroup, maxValueName)));

newArr

var newArr = function (objArr,maxValueGroup, maxValueName) {
  var arr = groupBy(objArr, maxValueGroup);
  var newArr = [];
  $.each(arr, function (key) {
    var maxVal = 0;
    var maxValItem;
    arr[key].forEach((element) => {
      if (element[maxValueName] > maxVal) {
        maxVal = element[maxValueName];
        maxValItem = element;
      }
    });
    newArr.push(maxValItem);
  });
  return newArr;
};

groupby

var groupBy = function (xs, key) {
  return xs.reduce(function (rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda