Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

70
Vistas
JavaScript - Check if value is already included in an array with sub-values

I have an array that has sub-values for each value of the array and I am trying to check if a sub-value already exists within the array. Normally I would just use array.includes() but that isn't working in this case. How would I check for sub-values? Here's what I mean:

The array structure: enter image description here

What I tried:

if (dataArray.includes(`classCRN: ${classesArray[i]}`) === false) {
  // ...rest of code...
}
6 months ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

You can use the array.some() method to check it there are the same sub-values.

if(dataArray.some(item => item.classCRN == classesArray[i])){
......
}
6 months ago · Santiago Gelvez Denunciar

0

Just loop through array to find property:

let arr = [
    {name: 'test', origin: 'something', value: 'bb123'},
    {country: 'Serbia', city: 'Belgrade'},
    {something: 'here', hey: 'there'}
]
console.log(isInnArray(arr,'there')) //true
console.log(isInnArray(arr,'mystring')) //false

function isInnArray(arr, findMe) {
    for(let i = 0; i < arr.length; i++) {
        for (const [key, value] of Object.entries(arr[i])) {
            if(value===findMe)
                return true
        }
    }
    return false;
}

6 months ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.