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
How to check if array of objects contains any value from array

Hi so i have an array of integers which i have been trying to check against my array of objects. I only want to check that ANY of the values in the array are within the objArray, it does not need to be ALL.

const objArray = [{id: 1},{id: 2},{id: 3}]

const array = '1,2'

This is what i tried but i think i am probably way off the logic, i end up getting false every time i run the below code.

stringToArray(array).includes(objArray.some(o => o.id))

Essentially my overall goal to is to validate that any return true and then continue with code otherwise throw an error.

function stringToArray() {
    return valueString.match( /(?=\S)[^,]+?(?=\s*(,|$))/g )
}

Thanks for you help! I keep getting false regardless and it may be to do with my function wrapping around the array?, My array was originally a string which i converted using regex to an array.

Thank you in advance!

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You're very close. Just flip your statement around.

objArray.some(o => array.includes(o.id))

const objArray = [{id: 1},{id: 2},{id: 3}]

const array = [1, 2];

console.log(objArray.some(o => array.includes(o.id)));

about 4 years ago · Santiago Trujillo Relatório

0

You were on the right track with your logic.

Although this is not the most efficient answer, it will work for ur case.

I believe you want to match all of the values, if I understand you correctly.

Therefore, one way to check if each id in the object is included in the array is to loop over the object.

If you want to much at least one, just change it to some instaed of every

const objArray = [{id: 1},{id: 2},{id: 3}]

const array = [1, 2,3]




const condition = objArray.every(objElement => {

// here you get acess to id here. so we check it againts the array 
 return array.includes(objElement.id)
})

console.log(condition)

about 4 years ago · Santiago Trujillo 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