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

168
Visualizações
JavaScript: remove duplicates from last in array of objects

I'm trying to eliminate the similar array of objects with the referent of name key in object with this function getUniqueListBy but it's giving the output by deleting the first duplicated elements in the array

I want to delete the duplicated from last by reference with the name

Sample input:

[
  { name:'shanu', id:1 },
  { name:'bhanu', id:2 },
  { name:'chary', id:3 },
  { name:'shanu', id:4 },
  { name:'rahul', id:5 }
]

Expected Output:

[
  { name:'shanu', id:1 },
  { name:'bhanu', id:2 },
  { name:'chary', id:3 },
  { name:'rahul', id:5      
]

What I did:

function getUniqueListBy(data, key) {
  return [
    ...new Map(data.map((item) => [item[key], item])).values(),
  ];
}

const data = [ { name:'shanu', id:1 }, { name:'bhanu', id:2 }, { name:'chary', id:3 }, { name:'shanu', id:4 }, { name:'rahul', id:5 } ];     
const arr1 = getUniqueListBy(data, "name");
console.log(arr1);

Current Output:

[
  { name:'bhanu', id:2 },
  { name:'chary', id:3 },  
  { name:'shanu', id:4 },
  { name:'rahul', id:5 }    
]

it's deleted the first item but I want to delete similar data from last.

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

0

The Map is overwriting the following values of the same key.

You can use a Set to store the added keys and return the final array using Array#reduce:

const getUniqueListBy = (data = [], key) => {
  const set = new Set();
  return data.reduce((arr, e) => {
    if(!set.has(e[key])) {
      set.add(e[key]);
      arr.push({...e}); 
    }
    return arr;
  }, []);
}

const data = [ { name:'shanu', id:1 }, { name:'bhanu', id:2 }, { name:'chary', id:3 }, { name:'shanu', id:4 }, { name:'rahul', id:5 } ];    
const arr = getUniqueListBy(data, "name");
console.log(arr);

about 4 years ago · Juan Pablo Isaza Relatório

0

const data =[
{
name:'shanu',
id:1
},
{
name:'bhanu',
id:2
},
{
name:'chary',
id:3
},

{
name:'shanu',
id:4
},
{
name:'rahul',
id:5
},
]

function getUniqueListBy(data, key) {
  return [
  ...new Map(data.reverse().map((item) => [item[key], item])).values(),
  ].reverse();
}

const arr1 = getUniqueListBy(data, "name");

console.log(arr1)

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