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

200
Visualizações
javascript - how to remove all single entries in an array

I have an array like this:

const data = [
  {"id": 3, "value": "a"},
  {"id": 3, "value": "b"},
  {"id": 4, "value": "a"},
  {"id": 8, "value": "d"},
  {"id": 1, "value": "d"},
  {"id": 2, "value": "d"},
  {"id": 5, "value": "z"},
  {"id": 8, "value": "h"},
  {"id": 8, "value": "b"},
]

and would like to delete all objects, where the value is not the same in one of the other objects. So, at the end I would like to have:

[
  {"id": 3, "value": "a"},
  {"id": 4, "value": "a"},
  {"id": 3, "value": "b"},
  {"id": 8, "value": "b"},
  {"id": 1, "value": "d"},
  {"id": 2, "value": "d"},
]

I tried several versions. How to check against something which you don´t know if it is there (later in the loop)? If I do check the list against itself in a loop, I am runing in the corner, that the array itself changes its length during the for loop... Is there a simple approach?

Thanks for any help!

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

0

You need two passes in your array: the first one will count the number for each values, the second one will remove the objects with lone values:

function removeLoneValues(data) {
  const values = {}

  data.forEach(datum => {
    if (values[datum.value] === undefined) {
      values[datum.value] = 0
    }
    values[datum.value]++
  })

  data.filter(datum => {
    return values[datum.value] > 1
  })

  return data
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve it like this:-

const data = [
  {"id": 3, "value": "a"},
  {"id": 3, "value": "b"},
  {"id": 4, "value": "a"},
  {"id": 8, "value": "d"},
  {"id": 1, "value": "d"},
  {"id": 2, "value": "d"},
  {"id": 5, "value": "z"},
  {"id": 8, "value": "h"},
  {"id": 8, "value": "b"},
]

let reqOutput = [];
for (let i = 0; i < data.length; i++) {
    if (data[i].value == "a") {
        reqOutput.push(data[i]);
    }
}
for (let i = 0; i < data.length; i++) {
    if (data[i].value == "b") {
        reqOutput.push(data[i]);
    }
}
for (let i = 0; i < data.length; i++) {
    if (data[i].value == "d") {
        reqOutput.push(data[i]);
    }
}

console.log(reqOutput);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.prototype.indexOf and Array.prototype.lastIndexOf alongside Array.prototype.filter and Array.prototype.map to achieve this:

const data = [
  {"id": 3, "value": 'a'},
  {"id": 3, "value": 'b'},
  {"id": 4, "value": 'a'},
  {"id": 8, "value": 'd'},
  {"id": 1, "value": 'd'},
  {"id": 2, "value": 'd'},
  {"id": 5, "value": 'z'},
  {"id": 8, "value": 'h'},
  {"id": 8, "value": 'b'},
];

const values = data.map((el) => el.value);

const nonUniqueData = data.filter((el) => values.indexOf(el.value) !== values.lastIndexOf(el.value));
console.log(nonUniqueData);

This code works by first creating a mapped array of only values, then by checking that the results of indexOf and lastIndexOf when finding that value in the mapped array are different we can check that the array is not unique. This heuristic is then used in a filter function to create a new version of your data array with all elements containing unique values removed.

If you need to sort your array based on its value, you can use Array.prototype.sort to do that based on whatever your values are. I've converted them to strings here so the code snippet will run.

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