Create an array mapping of locations and convert each location to its corresponding language

With Next.js, I have successfully retrieved the current locale and all available locales for a language selection menu:

const {currentLocale, availableLocales} = useContext(LangContext);
// currentLocale = "en-US"
// availableLocales = ["en-US", "de-DE"]

To facilitate the display of the full language name, I created this function:

const getLocaleDisplayName = ()=> {
    if (currentLocale === "en-US") { return "English" };
    if (currentLocale === "de-DE") { return "Deutsch" };
}

Now, I aim to transform availableLocales from

["en-US", "de-DE"]
to
["English", "Deutsch"]
. This not only enhances the visual appeal but also simplifies the language selection process for users. What would be the most effective approach to achieve this?

Answer №1

What do you think about using an object?

const languageAlias = {
  "en-US": "English",
  "de-DE": "German"
}
//...
anotherList.map(item => languageAlias[item])

Answer №2

If you want to create a new array without modifying the original one, you can utilize the Array.from method.

Make sure to pass arguments to the clAlias() function like this: clAlias(cl)

const originalArray = ["en-US", "de-DE"];
const clAlias = (language) => {
  if (language === "en-US") {
    return "English";
  }
  if (language === "de-DE") {
    return "Deutsch";
  }
};

const newArray = Array.from(originalArray, (element) => clAlias(element));
console.log(newArray);

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

Ways to showcase a website within an HTML document using a URL?

Is it possible to show 2 webpages on a single aspx webpage? For instance, When a user opens the link for www.mywebsite.com, I would like to display both www.google.com and www.bing.com on my homepage. Behind the scenes, I will call two separate URLs an ...

Apply a specific image layout once the drop event occurs

I have a container with 5 image pieces that need to be dropped into another container to complete the image. Once an image is dropped, I want to apply the style "position:absolute" so that it sticks to the previous image in that container. Although I have ...

Choosing a default selection in a nested v-for loop for a select box

Currently, I have a list of items that users can add new items to. Each item is required to have a select box, and the selected value from the select box should be assigned as the item's value. In an attempt to bind the select box to the item using t ...

The error message "TypeError: addNewUser is not a function in React.js onSubmit

What could be causing the error message "TypeError: addNewUser is not a function"? The issue arises when I complete the form and click save, displaying the error that addNewUser is not defined as a function. The problem occurs within the following code ...

Is it possible to generate multiple services from a single factory in Angular?

Recently, I developed a service called tableService using the following code: app.factory('tableService', [function() { var data = some data; var foo = function (){ //do something to data here }; return { data: data, ...

JavaScript compilation failure: Unhandled SyntaxError: Unforeseen token '>' in string variable within an if statement -- Snowflake

Looks like there's an issue with JavaScript compilation. The error message reads: Uncaught SyntaxError: Unexpected token '>' in HP_SEARCHCBHMESSAGES at ' if (Fac123 <> "") ' position 1.. Strange how SF is not a ...

Receive a notification for failed login attempts using Spring Boot and JavaScript

Seeking assistance with determining the success of a login using a SpringBoot Controller. Encountering an issue where unsuccessful logins result in a page displaying HTML -> false, indicating that JavaScript may not be running properly (e.g., failure: f ...

Is it possible to restart an animated value in React Native?

I'm facing an issue in my react native app where I have created a simple animated progress bar, but I am unsure how to reset the animation. I attempted the following approach without success: progressValue = 0; How can I reset the animation? Also, w ...

Manipulate HTML content from JSON data in JavaScript without using jQuery

The information stored in the texts.json file: [{ "PageTextKeyId": 1, "PageTextKeyName": "page-first-text", "PageTextValueName": "Lorem ipsum dolor sit amet" }, { "PageTextKeyId": 2, "PageTextKeyName": "after-page-first-text", "PageTextValueNa ...

Returning data to be displayed in Jade templates, leveraging Express and Node.js

Yesterday, I had a question. Instead of using next() and passing an Error object, I decided to figure out what it was doing and replicate it. So now, when someone logs in and it fails, I handle it like this: res.render("pages/home", { ...

Tips on modifying the interface based on the selected entry using jQuery

I am attempting to display a text when different options are selected from the dropdown list. Here is the code for the drop-down list: <div class="dropdown"> <select class="form-control" id="ltype" name="ltype"> <option value=""&g ...

After ReactDOM is used, the JavaScript code is no longer functioning properly

While working on my latest project, I utilized react.js and encountered an issue where javascript seemed to stop working when using ReactDOM to render a class extended from React.Component. I noticed that the alert() method would not work if it was placed ...

What are the steps to incorporate dynamic routing within Next.js?

After converting my React.js app to Next.js, I am now looking to implement routing to my blog details page upon clicking a card on the blog page. The data is being fetched from a Heroku API. In React, I used react-router-dom for this purpose. Can anyone ...

In my experience, I have encountered issues with certain routes not functioning properly within Express

I am currently working on developing a tic-tac-toe game and looking to store user data in a database. However, I am facing an issue with the router I intended to use for this purpose as it is returning an 'Internal server error message (500)'. B ...

trouble with fetching data

Within my Backbone view, I have the following code snippet: var BookView = Backbone.View.extend({ initialize: function() { this.render(); }, render: function() { this.model.fetch({ success : function(model, resp, opt) { alert(this.$el. ...

Challenges in using Three.js on mobile devices with orientation capabilities

I'm currently running tests on a web application built using three.js, and it seems that users on tablets, especially Android devices, are experiencing some unusual behavior within the scene. https://i.sstatic.net/FkFi3.jpg Below is how the view sho ...

Cleaning out input fields in React applications

When I click on the <button type="submit" className="btn btn-primary">Create</button>, a card with hero is generated. Then, using .then(() => clearFields()), I am able to clear the input fields so that clicking the Create ...

I am looking to dynamically load a script only after retrieving specific data from a JSON file in Next.js

I am trying to ensure that the Script tag loads after the data.post.content is loaded within the HTML. Specifically, my goal is to execute the MathJax.js script inside the HTML. This is the code I have: return ( <div> <h1>{data.post ...

Storing HTML elements in a database using jQuery's html() method

I have encountered a dilemma that has left me stumped after extensive online research. I have a dynamically generated webpage and need to save the entire appended body of the page to a database using PHP and MySQL. My current approach involves using the fo ...

Determine the width of two inline input fields

Showing two inputs side by side: +------------+ +--------------------------+ | ID='inputA'| | ID='inputB' | +------------+ +--------------------------+ +------------------------------------------+ A ...