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

106
Visualizações
Check if array of objects contains all the values of an array of integers

Is there a way to know if all the values of an array of integers are in an array of objects? If the values of the array of integers are all in the array of objects, as return I intend true and false the opposite, that is, the values are not all present in the object.

DEMO

 Sizes = [
    {
      id: 1,
    },
    {
      id: 2,
    },
    {
      id: 3,
    },
    {
      id: 4,
    },
  ];

  Number = [1, 2, 3];
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Use every + some:

Number.every(n => Sizes.some(obj => obj.id === n))

 Sizes = [
    {
      id: 1,
    },
    {
      id: 2,
    },
    {
      id: 3,
    },
    {
      id: 4,
    },
  ];

  Number = [1, 2, 3];
  
console.log(Number.every(n => Sizes.some(obj => obj.id === n)))

about 4 years ago · Juan Pablo Isaza Relatório

0

You could map the values to a new array of values to check, then check if the values includes the numbers.

const sizes = [{
    id: 1,
  },
  {
    id: 2,
  },
  {
    id: 3,
  },
  {
    id: 4,
  },
];

const check = (ns, szs) => {
  const nsToCheck = szs.map((x) => x.id); // get values
  for (const number of ns) {
    if (!nsToCheck.includes(number)) return false; // return false if !include
  }
  return true; // otherwise return true
}

const numbersWillSucceed = [1, 2, 3, 4];
const numbersWillFail = [1, 5];

console.log("Will succeed: ",check(numbersWillSucceed, sizes));
console.log("Will fail: ",check(numbersWillFail, sizes));

about 4 years ago · Juan Pablo Isaza Relatório

0

You can easily solve this problem with functions already existing for JavaScript and Arrays.

With:

const test1 = [
    {
      id: 1,
    },
    {
      id: 2,
    },
    {
      id: 3,
    },
    {
      id: 4,
    },
];

and:

const test2 = [1, 2, 3];

One can use:

test2.every(t2 => test1.find(t1 => t1.id === t2) !== undefined)

to check if every element from test2 exists in test1. It simply tests (every) whether every element from test1 is contained in the array test2 (with find).

Please do not use variables like "Number", since Number already exists in JavasScript, see also https://eslint.org/docs/rules/no-shadow#builtinglobals

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