Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
How do I check if a Map of Strings to Arrays of Objects has a given value within the Object?

I want it to check and then add a book if it doesn't exist. I am having trouble accessing the index('Horror') part.

const bookMap = {
    'Horror' => [{
        id: 9798721052927,
        title: 'Dracula',
        author: 'Brahm Stoker'
    }],
    'Romance' => [{
        id: 9798721052927,
        title: 'Love Story',
        author: 'Brahm Stoker'
    }]
}

bookMap.get('Horror').filter(e => e.title === 'Dracula')
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The => for objects is used in PHP, not JavaScript. JavaScript uses :.

To access the value, you use ['name'] or .name.

const bookMap = {
  'Horror': [{
    id: 9798721052927,
    title: 'Dracula',
    author: 'Brahm Stoker'
  }],
  'Romance': [{
    id: 9798721052927,
    title: 'Love Story',
    author: 'Brahm Stoker'
  }]
};

let books = bookMap.Horror.filter(e => e.title === 'Dracula');

console.log(books);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your object is not a valid javascript object. The correct way:

const bookMap = {
      'Horror': [
        { id: 9798721052927, title: 'Dracula', author: 'Brahm Stoker' }
      ],
      'Romance': [
        { id: 9798721052927, title: 'Love Story', author: 'Brahm Stoker' }
      ]
    }
    
    // then
    // access like this
    console.log(bookMap.Horror.filter(e => e.title === 'Dracula'))
    
    // or this
    console.log(bookMap["Horror"].filter(e => e.title === 'Dracula'))
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

As you want to add if that items not available then you can do following

let bookMap = {
    'Romance': [{
        id: 9798721052927,
        title: 'Love Story',
        author: 'Brahm Stoker'
    }]
};

Object.keys(bookMap).forEach(function(key) {
    if (key !== 'Horror') {
        bookMap['Horror'] = [{
            id: 9798721052927,
            title: 'Dracula',
            author: 'Brahm Stoker'
        }]
    }
});

console.log(bookMap)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda