A guide on converting JSON strings into arrays using Javascript

In my JavaScript program, I have a Mocha test that checks if all available currencies are displayed in a drop-down list:

 it('displays all available currencies in drop down list', () => {
    return invoiceEditPage.details.currencyDropDown.dropDown.waitForEnabled()
    .then(() => invoiceEditPage.details.currencyDropDown.click())
    .then(() => getEnabledCurrencies(tenantUsers.admin.authUser))
    .then((listOfCurrencies) => console.log(listOfCurrencies["name"].split(",")))
    //.then((listOfCurrencies) => this.getCurrencyFromJSON(listOfCurrencies))
    //.then(() => console.log(invoiceEditPage.details.currencyDropDown.dropDownContents))
    //.then((listOfCurrencies) => assert.strictEqual(listOfCurrencies, invoiceEditPage.details.currencyDropDown.dropDownContents))
    .then(() => invoiceEditPage.details.currencyDropDown.dropDownMask.click());
});

When I use the following line:

.then((listOfCurrencies) => console.log(listOfCurrencies))

I see a JSON string being printed out like this:

[ { displayText: 'USD$',
name: 'US Dollar',
symbol: 'USD$' },

and so on.

My goal is to extract an array of strings containing the names of all the JSON objects, for example:

["US Dollar", "Canadian Dollar", "Australian Dollar"]
.

However, when using the above line, I encounter an error message stating:

"undefined: Cannot read property 'split' of undefined"

Even after trying JSON.parse(), I receive an Unexpected token o error, indicating that "listOfCurrencies" is already a string. Can anyone help me understand what's causing this issue?

Thank you

Answer №1

One way to efficiently extract the currency names is by utilizing the map function.

.then((listOfCurrencies) =>      
    console.log(listOfCurrencies.map( currency => currency.name ));
);

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

Add the information to the current JSON file

I'm currently working with an existing JSON file and attempting to add a string into it. However, whenever I write to the JSON file, the new line characters disappear and the formatting gets altered. Check out the code snippet below: #!/usr/bin/pyth ...

Calculating the Return on Investment (ROI) for Masternodes: A Comprehensive

Is there a way to calculate the ROI of each masternode without using traditional APIs? I will reveal how this can be done. Utilizing data from CoinMarketCap, you can access valuable information about prices and market capitalization. However, determining ...

executing jQuery toggle on freshly generated content

I have a webpage that I designed using jQuery. Within this page, there is a table with rows assigned specific color classnames (e.g., tr class="yellowclass"). Users can filter the rows by clicking checkboxes to show/hide tables of different colors. The i ...

Leverage the power of the YouTube API to determine if a video is able to be embedded

As I delve into the YouTube Data API v3 to determine if a particular video is embeddable, I have come across the status.embeddable property that seems crucial. By making a request like this: https://www.googleapis.com/youtube/v3/videos?id=63flkf3S1bE& ...

Utilizing Vue.js and Webpack to Handle Requests for Multiple Incorrect Resource Links

After utilizing Vue.js single file components to construct a website and appreciating the modular approach, I've encountered an issue. The browser appears to be requesting multiple versions of resources instead of just one URL for each. HeaderBar.vue ...

Troublesome problem encountered when sending a JSON object to a C# method, specifically when the method requires a collection as a parameter and is being handled

Encountering a peculiar issue when passing the given data: queueNotificationData = { StartDate: that.batchData.StartDate.valueOf(), StartTime: that.batchData.StartTime.valueOf(), EndDate: that.batchData.EndDate.valueOf( ...

Guide on processing a hefty JSON file by rounding decimals to the nearest integer and calculating the average of y values when there are duplicate x values

I am currently exploring ways to efficiently filter and parse through extensive JSON data sets. My main goal is to round the x values to the nearest integer. In cases where there are duplicate entries, I aim to calculate the average of the y values while ...

accessing a webpage using an ionic application

I currently have a responsive website, but I am looking to turn it into an app in order to utilize push notifications. Right now, I am using the inappbrowser plugin to open my website with this code: <a href="#" onclick="window.open('http://www.ex ...

When attempting to connect to the MongoDB cloud, an unexpected error arises that was not present in previous attempts

npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="650800170b4816001713001725544b554b55">[email protected]</a> start > nodemon index.js [nodemon] 3.0.2 [nodemon] to restart at any time, enter ...

Vue.js has a feature where it automatically closes the form tag within a for loop

In my Vue.js application, I have created a table where each row is a form with a submit button. This is the code snippet I am using: <div id="admin-user"> <table class="table"> <tr v-for="(user, index) in users"> < ...

Combining a JavaScript NPM project with Spring Boot Integration

Recently, I built a frontend application in React.js using NPM and utilized IntelliJ IDEA as my IDE for development. Additionally, I have set up a backend system using Spring Boot, which was also developed in IntelliJ IDEA separately. My current goal is t ...

Strategies for effectively managing and validating Mongoengine objects containing ReferenceFields

Hey there! I'm currently working on developing a REST API using Flask-RESTful, MongoEngine, and marshmallow. I've run into some challenges when it comes to testing my models that contain ReferenceFields. Specifically, I have a model called "Pra ...

Replicate a click using jQuery to retrieve a filtered multigallery based on the URL

I have a website that was built using Elementor for a photographer. The site features an Elementor Multigallery with a filter at the top. I am looking to create external links that will direct users to specific subpages showing only filtered items. For ex ...

What is the best way to add an insert button to every row?

Take a look at the image provided. When I click on any register button, the last row is being inserted instead of the desired row. What I'm aiming for is this: when I click register, the entire selected row should be stored in a separate table. Whe ...

What steps should I follow to obtain code coverage data in my Aurelia application with the help of karma?

After creating my Aurelia app using the Aurelia CLI (au new), I wanted to set up code coverage, preferably with karma-coverage, but was open to other options as well. First, I ran npm install karma-coverage --save-dev and then copied the test.js task over ...

Insert an element at the start of a sorted array object

I am currently working on a small application that has the following structure: Posts -> Post -> Comments -> Comment. The Posts component sends a request for an array of posts sorted by date in descending order. Each post also fetches its comments through ...

Simple Mobile-Friendly Tabs - mobile tabs are always visible

My JavaScript skills are not the best. I have been using Easy Responsive tabs and encountered an issue on the mobile version where clicking does not hide content, but only removes the "d_active" class and adds it to another element. I believe that if the ...

Issues with FullCalendar functionality in CakePHP 1.2.5 are arising when using jQuery version 1.4.1

Currently, I am encountering an issue with fetching events data through a URL that returns JSON data. Surprisingly, the code works flawlessly with jQuery 1.3.2, however, it throws errors when using jQuery 1.4.1. The specific error message displayed in the ...

Does the entire state get replaced every time a change is made if the state is immutable?

Is it necessary to replace the entire state if it is immutable? Otherwise, wouldn't mutating the state occur? Are top-level keys maintained as distinct immutable objects? Wouldn't changing anything necessitate replacing the entire state by defin ...

Ways to conceal a grid item in Material UI framework

My goal is to hide a specific grid item for a product and smoothly slide the others in its place. Currently, I am using the display:none property but it hides the item instantly. I have already filtered the products and now I want to animate the hiding of ...