Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!