Is there a quicker method to access an object's ID?

Within my array of objects, the structure is as follows:

var locations = [
[{id: 1, lat: 51.52376322544537, lng: 5.13785702262885, content: 'Title A'}],
[{id: 2, lat: 51.52358632767757, lng: 5.137921395645208, content: 'Title B'}],
[{id: 3, lat: 51.52343946863126, lng: 5.138093057022161, content: 'Title C'}],
];

To access the id's, I currently employ this method:

if (id === 7 || id === 13 || id === 15 || id === 16) {
        marker.setIcon(icon4);
    }

I attempted a shorter approach like so:

if (id.toString().indexOf(['7','13','15','16'] > -1)) {
        }

However, this method is not functioning properly. If anyone can offer guidance on a more efficient solution, it would alleviate my frustration with the current code implementation.

Answer №1

You have the option to modify it as follows:

if ([7,13,15,16].indexOf(id) > -1 ) {
        marker.setIcon(icon4);
    }

Answer №2

Consider using the switch statement instead of multiple if conditions:

switch(id)
{
case 7:
case 13:
case 15:
case 16:
marker.setIcon(icon4);
break;
}

By utilizing the switch statement, future updates to id values can be easily accommodated with just one additional case. This approach offers simplicity and efficiency.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting the UrlAction in an Asp.Net Core Controller: A step-by-step guide

There is an input box for searching on the website... The following HTML code snippet shows how the search functionality is implemented: <form id="searchForm" asp-controller="Product" asp-action="SearchProduct" method=&quo ...

The dropdown options for the input type file in Vuejs PWA are not appearing

I have created a Vue.js progressive web app that allows users to easily upload images from their mobile phones. While the app typically functions well, there is an issue where upon downloading the app to the homescreen, the image upload feature sometimes b ...

Personalized Jasmine matchers for a Protractor/WebDriverJS component

Greetings fellow developers, I'm looking to create a custom matcher for Protractor/WebDriverJS element. Can anyone assist in improving my current code? Here is what I aim to include in the specs files: var button = element(by.tagName('button&a ...

What is the best way to include an external JavaScript file in a Bootstrap project?

I am brand new to front-end development and I'm attempting to create an onClick() function for an element. However, it seems like the js file where the function is located is not being imported properly. I've tried following some instructions to ...

Transmit a PDF byte array from JavaScript to PHP using Ajax, then utilize the PHP file to send an email

I am facing a particular issue. Currently, I am utilizing the pdf-lib library for JavaScript to generate a PDF. The last step involves creating a uint8array: const pdfBytes = await pdfDoc.save() My goal is to transmit these pdfbytes via AJAX to a PHP fi ...

"Header background image gradually fades out and disappears as you scroll, revealing a bottom gradient effect in a React application

When I scroll, my header image fades away. I used JavaScript in my React app to achieve this effect: useEffect(() => { const handleScroll = () => { if (parallaxDiv.current) { parallaxDiv.current.style.backgroundPositionY = `${ ...

What is the best way to implement restful instead of query syntax in a $resource factory?

Recently, I set up a $resource factory like this: (function() { 'use strict'; app.factory('WidgetFactory', [ '$resource', function($resource) { return $resource('http://localhost:8282/widget/:widgetId&apo ...

Validating Date Input in HTML and JavaScript Text Fields

When designing my form, I wanted to ensure that users could only input digits and hyphens in a text field where they enter a date. However, I encountered some difficulty implementing this feature. Currently, the field only accepts digits and removes any le ...

Receive the outcome once the form is submitted

Can someone provide quick assistance? I am looking to allow users to upload a CSV file for me to parse and analyze. Once the processing is done, I need to display the results back to the users. While uploading the file, I also need to ensure that the file ...

How can I pass an object into a function's prototype in Javascript?

In my quest to delve deeper into JavaScript, I've embarked on creating my very own MVC library. My current investigation involves examining the source code of backbone.js which can be found here. When defining a Collection in backbone.js, it involves ...

Creating a Cascading State and City Dropdown Using Knockout JS and JSON Data

When the user selects a state from two fields, state and city, a call is sent to the server returning JSON data. This data needs to be used to update the city field in knockout, but there seems to be an issue with the syntax of getting the ajax data. Here ...

What is preventing us from setting a child's width and height to match that of the parent element?

Parent element is a div with a width of 300px and a height of 40px, containing a child input. In the following code snippet: myinput = document.getElementById("e2"); myinput.style.cssText ='width:100%;height:100%;padding:0px;margin:0px;' div{ ...

Show a property from the local storage as an option in ng-options

Within my Angular application, I keep crucial victim data in local storage. This data is then showcased in the view where it can be altered (via a <select>): <h1>Victim #{{victim.numero}}</h1> <label>Victim status</label> &l ...

The printed borders do not correspond with the nodes

I am encountering an issue with my program that reads a file containing two columns of numbers, sorts them, and creates three tables: one for nodes individually, another for all edges, and the third for the number of edges per node. The problem arises when ...

methods for extracting header information using JavaScript

Is there a way to retrieve values from the header URL using JavaScript? Consider this scenario: www.example.com/content.html?param=value How can one extract this information upon redirecting to the page content.html? What techniques could be employed f ...

Function in Typescript that can return multiple data types

I recently started learning about TypeScript and its concepts. During my practice sessions, I encountered a problem that left me puzzled. There is a function named `functionA` which returns an object based on the response received from an API. type Combina ...

Tutorial on inserting a picture into muidatatables

Important Note: The image stored in the database is called "profileImage." I am looking to create a dynamic image object similar to other objects. When I input this code: { label: "Image", name: "profileImage" }, it simply displays the image endpoint as ...

Tips for retrieving JSON data using ajax with jPut

Recently, I stumbled upon a handy jQuery plugin called Jput that allows for easy appending of JSON data to HTML content. You can check it out here. However, I am curious about the process of sending and retrieving JSON data via AJAX. Any insights or guida ...

Developing a React-based UI library that combines both client-side and server-side components: A step-by-step

I'm working on developing a library that will export both server components and client components. The goal is to have it compatible with the Next.js app router, but I've run into a problem. It seems like when I build the library, the client comp ...

Identifying distinct values in each row using 2D vectorization under specific conditions

Here is an array along with a function definition: import numpy as np a = np.array([[2, 2, 5, 6, 2, 5], [1, 5, 8, 9, 9, 1], [0, 4, 2, 3, 7, 9], [1, 4, 1, 1, 5, 1], [6, 5, 4, 3, 2, 1], [ ...