What is the best way to determine if the index of an array does not match a specific argument

Currently enhancing my Javascript skills by developing small text RPG games using HTML pages :) It's great practice, as I'm constantly seeking to expand my knowledge and improve the code in my projects.

At the moment, one of my goals is to implement language functionality into my project through a function;

language.addLang = function(name,id){
    if (id != this.log[id]) {
        this[name] = new Object;
        this.log[id] = name
        
    }
    
}

An edit: I want to check if I can compare an array index number with an argument id number. The 'name' in the argument represents the language name, like English with a string value. And the 'id' signifies which index of the array it will occupy. It also includes a conditional statement that will translate all text into that language for future use.

For those curious about what 'log' denotes;

language.log = [
  undefined, 'English','Swedish','Japanese','German'
]

To sum up; As mentioned above, the length of the log array is currently 5. For instance, if I wish to add a new language to my project for translation purposes, I can call language.addLang('Chinese',5). This action will insert the string 'Chinese' into the array at index number 5. However, trying to add it to an existing index will result in an error instead of replacing 'Chinese' with another language.

I hope this clarification provides better insight compared to my previous post. While I can manually translate the content instead of creating a new object for each language, it would be incredibly useful to be able to compare the argument number with the index number.

Answer №1

It seems that @Charliefl is correct in pointing out the solution, however, you can tweak it to better suit your specific case by making modifications to your existing code.

var IncludedLanguages = [ 3 , 4 , 5 ] 
var LangObjectsArray = [] 


language.addLang = function(name,id){
   
   if ( IncludedLanuages.includes( id  ) != true )   {
      
            
        
      var obj = { "name" : name , "id" : id  }  
        LangObjectsArray.push( obj ) 
        IncludedLanguages.push( id )
    }
    
}

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

What is the best way to access the current value and name of a textbox in real-time using

How can I retrieve the value of a textbox in JavaScript using onblur and on keyup events, while also performing real-time checking for each individual textbox using "this keyword"? Here is my JSFiddle link. Can you assist me in modifying it? ...

updateStatusCallback function is not defined in the Facebook example using jQuery

I've been trying to incorporate Facebook integration into my HTML code, specifically adding features like Facebook login and sharing functionalities. However, I've hit a roadblock in the process. Even after searching extensively for solutions, I ...

Having trouble displaying image links with React Router

I have been working on designing a mock Instagram page, complete with icons for "Home", "Inbox", "Explore", "Notifications" & "Profile Icon" in the top menu bar. Initially, these icons were static and did not have any functionality, but they would displa ...

When I click on the left side menu, it should automatically scroll upwards

How can I make the right side page scroll go up when clicking on the left side menu? Please assist me with this issue. https://i.sstatic.net/doamN.jpg ...

What is the best way to perform the subtraction of sparse matrices?

I am currently facing an issue with array subtraction in Python: import numpy as np from scipy.sparse import csr_matrix a = np.array([[1, 2], [3, 4]]) b = a[:, None] - a[None, :] sum_ = np.sum(b, axis=-1) print(sum_) The above code works fine. However, ...

The user table data is not being retrieved successfully on Hostinger's platform

On my WordPress site, I have a script that retrieves the user ID from the user table. This script runs smoothly on my local server and all other servers except for Hostinger. I'm wondering why this script is not functioning properly on Hostinger? Th ...

``There seems to be an issue with JQuery.Ajax not properly displaying on Samsung Smart

I attempted to use JQuery.Ajax to interact with my webservice Below is the code snippet: Main.onLoad = function() { // Enable key event processing this.enableKeys(); widgetAPI.sendReadyEvent(); //$("#h2Test").html("Change On Text"); ...

The Firebase Function encountered an error: TypeError - it is unable to read the property 'replace' as it is undefined

I'm currently following Aaron Klasner's guide on Hackernoon for setting up a Medium feed in react. To test it locally, I need to find a way around the CORB that Medium uses to prevent local requests. I tried using a firebase function, but encou ...

Adjust the opacity of a MeshBasicMaterial in real-time

<script type="importmap"> { "imports": { "three": "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="384c504a5d5d784e0816090e0a1608">[email p ...

Invoke a function or variable based on a string parameter within a JavaScript/React function dynamically

I am currently exploring ways to dynamically call a function based on a string or reference a variable using a string. For example: import React, {useState} from 'react' const array = [ { text: 'count1', setFunctionName: &apo ...

What is the process for resolving arguments in UI-Router's resolve function?

There seems to be a gap in my understanding of UI-Router and angular's documentation, so forgive me if this seems naive, but here goes: On http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider, there is an example resolve fu ...

Connect the incoming http request information to a different controller

My navigation menu has the following structure: <div class="collapse navbar-collapse" id="admin-side-nav" ng-controller="AdminNav"> <ul class="nav nav-pills nav-stacked"> <li><a href="/admin/leaderboard/{{gameId}}">Lead ...

Which method is more effective: utilizing AJAX to retrieve page elements, or just toggling their visibility?

When it comes to web development, particularly in jQuery, should I preload my page and use jQuery to manipulate the DOM, or should I do it the other way around? This debate involves: <div id="item1" ></div> <div id="item2"></div> ...

What exactly happens when you use the increment operator with an array name?

Trying to understand how values move around with arrays, I found this code helpful. Everything is clear until the part that mentions ++mode[i][0] towards the end. I'm puzzled about what exactly this is incrementing. Just to clarify, this code isn&apos ...

Ajax insertion was successful, but the database records are empty

Why is my code able to save data to the database using Ajax, but all rows are empty? Here is My Form: <form name="frm" id="frm" action=""> <div class="form-group"> <label for="namaproduk">Product Name</label> <input t ...

Arranged array in PHP based on month name and year

Here is an example of an array: $ar =['March 2018', 'March 2018', 'November 2017', 'December 2017', 'January 2018', 'January 2018', 'February 2018' ...

What is the best way to extract a nested array of objects and merge them into the main array?

I've been working on a feature that involves grouping and ungrouping items. A few days ago, I posted this question: How can I group specific items within an object in the same array and delete them from the core array? where some helpful individuals ...

How to pinpoint a particular element in a parsed json using Ruby

I have a JSON string that I need to parse using Ruby. The string looks like this : [ { "id": 1, "player_id": "not_this_one\n", "highscore": 23 }, { "id": 2, "player_id&qu ...

Unable to trigger click event on SVG path element

Hey there, I'm currently working on a project and I'm having some trouble with the click event for my SVG paths. $path.map(function(){ $(this).mouseenter(function(){ $count = $path.attr('data-like'); $('#coun ...

I've been diving into webpack and experimenting with making an api call, but I'm hitting a roadblock. Let me share with you my code

This section pertains to the server const dotenv = require('dotenv'); dotenv.config(); // Setting up environment variables const express = require('express'); const bodyParser = require('body-parser'); const cors = require(&a ...