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

156
Visualizações
Return Boolean if element of first array is in element of second nested array

I have two different types of arrays firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}].

The second object looks like this secondObject = {someAttribute: bla, numbers: [23, 26, 27, 28}

Now i want to check if at least one of the provided number fields of the firstArrayObject is not part of the secondObject's numbers Array. If it is not part, stop the search progress and return true. In the provided example it should return true, because "25" is not part of secondObject.numbers

Im struggling with all the different methods es6 provides like includes, filter, some.

Hopefully anyone can help me out here.

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

0

You're on the right track with some:

const result = firstArrayObject.some(
    ({number}) => !secondObject.numbers.includes(number)
);

const firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}];
const secondObject = {someAttribute: "bla", numbers: [23, 26, 27, 28]};

const result = firstArrayObject.some(({number}) => !secondObject.numbers.includes(number));

console.log(result);

That says: "Do some (any) of firstArrayObject's elements match the condition 'number is not included in the array `secondObject.numbers'?"

I used destructuring there, but you don't have to:

const result = firstArrayObject.some(
    (element) => !secondObject.numbers.includes(element.number)
);

If secondObject.numbers were a very larger array, you might start out by making a Set of the known numbers, because the has function of a Set is required (by the JavaScript specification) to have better performance than the linear lookup time of includes:

const numbers = new Set(secondObject.numbers);
const result = firstArrayObject.some(
    ({number}) => !numbers.has(number)
);

const firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}];
const secondObject = {someAttribute: "bla", numbers: [23, 26, 27, 28]};

const numbers = new Set(secondObject.numbers);
const result = firstArrayObject.some(
    ({number}) => !numbers.has(number)
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Hope this helps!! Used forEach and includes function of array. A good resource to learn javascript array functions.

var answer = false;
const firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}];

const secondObject = {someAttribute: "bla", numbers: [23, 26, 27, 28]};

function func(obj) {
    if(!secondObject.numbers.includes(obj.number)) answer = true;
}

firstArrayObject.forEach(func);
console.log(answer)

Edit: func can be created as the forEach parameter itself, although the above implementation might make it more readable for beginners.

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