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

175
Visualizações
Get random value from array if it doesn't exist in another array

I have two arrays and I want to get a random new value from Array1, if the value isn't included in Array2 already.

const array1 = ["Banana", "Orange", "Apple", "Mango"];
const array2 = ["Banana", "Mango"];

const randomElement = array1[Math.floor(Math.random() * array.length)];

I have already have a existing variable called randomElement, that finds a random element in Array 1 but it doesn't look at whether the element already exists in Array 2 or not.

Ideally the variable randomElement should only currently output either "Orange" or "Apple" since they are the ones missing in Array 2. How do I go on about this task the easiest?

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

0

You need to compute the array having elements in array1 but not in array2 first, you can do this using Array#filter and Array#includes:

const array1 = ["Banana", "Orange", "Apple", "Mango"];
const array2 = ["Banana", "Mango"];

const array = array1.filter(e => !array2.includes(e));
const randomElement = array[Math.floor(Math.random() * array.length)];

console.log(randomElement);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can calculate the difference set (elements that are in array1 but not included in array2) between the two sets and use it to pick the next element:

const diff = array1.filter((v) => !array1.includes(v));
const randomElement = diff[Math.floor(Math.random() * array.length)];

console.log(randomElement);
about 4 years ago · Juan Pablo Isaza Relatório

0

Your current implementation is just showing a random value from the array. One way to fix this is to wrap this inside a loop. Keep on getting the value and do not return until you find a value which is not in the array2. That could go on forever (in worst case scenario).

In another approach:

You will need a third array which will be a filtered version of your array1. Run all the operations on that and you should be good.

const array1 = ["Banana", "Orange", "Apple", "Mango"];
const array2 = ["Banana", "Mango"];
const filteredArr = array1.filter(x => !array2.includes(x));
const randomElement = filteredArr[Math.floor(Math.random() * filteredArr.length)];
console.log(randomElement);

Methods used : filter() - to filter array elements based on a condition.

includes() - to check if a value exists in an array

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