Techniques for converting a variable into an array in vue.js

I am attempting to output a variable as an array like

[ 'Google', 'Facebook', 'Twitter', 'Apple', 'Oracle' ]

However, the result I am receiving is:

Google
Facebook
Twitter
Barishal
Oracle

The code I have in setup() :

const stringOptions = axios.get('https://api.com').then(response 
 => {
        Object.keys(response.data.divisions).forEach(key => {
          var a = response.data.divisions[key].name
          console.log(a)
        })
    })

Answer №1

To achieve the desired outcome, you can utilize the combination of Object.keys() in conjunction with Array.map() method.

Consider the following approach:

const companies = {
  0: {
    name: 'Google'
  },
  1: {
    name: 'Facebook'
  },
  2: {
    name: 'Twitter'
  },
  3: {
    name: 'Barishal'
  },
  4: {
    name: 'Oracle'
  }
};

const result = Object.keys(companies).map(key => companies[key].name);

console.log(result);

Answer №2

Here's a different method that utilizes Object.values combined with Array.map() for the same task.

const divisions = {
  0: {
    name: 'Google'
  },
  1: {
    name: 'Facebook'
  },
  2: {
    name: 'Twitter'
  },
  3: {
    name: 'Barishal'
  },
  4: {
    name: 'Oracle'
  }
};

const res = Object.values(divisions).map(division => division.name);

console.log(res);

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

Filtering a table based on user selection: specifying column, condition, and value using JavaScript

I am new to JavaScript and looking to implement real-time filtering on a table based on user input. My project utilizes Django and PostgreSQL for storing the table data. template <form action="" method="get" class="inline" ...

jQuery triggers change event twice when radio group is manually modified

After selecting "A&L" in the dropdown menu, the radio group is hidden and its value is changed to "n". I attempted to trigger the change event to make the "Hello" message disappear as well, but it seems to be executing twice - once correctly and then ...

Simultaneous Push and Insert operations with Vue.js and PHP

I am looking to add a value into an Array and have it displayed instantly without the need to refresh the entire page. app.todoList.push(this.todo) This line accomplishes that task. Simultaneously, I also want to insert this value into a Database. The i ...

Tips for effectively implementing React.usecallback

Looking for a way to rewrite the handleClick method using React.useCallback in a function called Parent, which is triggered by a button click event in React and TypeScript. function Parent () { const [isOpen, setIsOpen] = React.useState(false); ...

Is there a way to retrieve a jQuery element from a regular JavaScript selection?

When a user selects text on my page, I pass the selected object to a function. In this function, I need to retrieve the appropriate text note using jQuery. How can I accomplish this? Here is the function: var manipulator = function (coreObject) { con ...

Automatically resizing images in a canvas when their resolution exceeds the canvas size in HTML5

I am currently using Html5, fabric.js, and JavaScript to upload multiple images to a canvas. However, there are instances where the uploaded image size exceeds that of the canvas. I am looking for a way to ensure that the image is set to a fixed size. v ...

Selenium WebriverJS does not support right mouse button clicks in PhantomJS

Currently, I am utilizing Selenium in combination with WebdriverJS to automate a straightforward web application. However, an issue arises when transitioning from Chrome to PhantomJS, resulting in the test not functioning as intended. Specifically, the rig ...

The functionality for transferring ERC20 claim tokens seems to be malfunctioning

I am facing an issue with my contract while trying to execute the claimFreeToken function. Even though the contract does not have enough tokens, the function does not return an error and the token also does not get received. Can anyone point out where I ...

An effective method for converting a string into a two-dimensional array using JavaScript

One challenge I am facing is checking from a string if a specific fruit has the correct amount on a given date. I've managed to achieve this by converting the string into a 2D array and iterating over the columns. While this method works, I can't ...

"Trouble in paradise: Angular.js fails to make a lasting impression

As a beginner in Angular, I am attempting to create a simple Hello World application. Below are my source files: <html > <head> <script src="./node_modules/angular/angular.js"></script> <title>ngClassifieds</tit ...

React NextJS: Unable to retrieve cookies or properties post redirection

Within my nextJS application, when a user logs in on the login page, a cookie is created with a token and then they are redirected to the main page which utilizes the main component. However, within the Main component, I am encountering an issue where the ...

Guide to importing external CSS styles into Angular 2 using the require method

I'm facing an issue with loading an external css file in different environment files. My setup includes two environments: development and production. Loading the css file locally works fine in development mode, but not in production mode. environment ...

The axios response cannot be awaited in Jest unit tests

While unit testing my Vue-Electron project with Jest, I encountered an issue where a chain of events triggered by a fake button click was not functioning as expected. In the midst of this chain, an axios request was made to a server to retrieve data, but t ...

Upload the image data into the input file tag

Is there a way to take a string code stored in a JavaScript variable that is retrieved from the Cropit plugin (used for cropping images) and then set this information into an input file tag? I want to be able to send it via AJAX to PHP. Here is the JavaSc ...

When a button is clicked, unleash the magic of music using JavaScript

Why isn't the button onclick working for the mp3 file I uploaded to GitHub? I even tried using the link through https://voca.ro/1dcHxW3v1oQA, but it still wouldn't work. function play() { var audio = new ...

Can you assist in resolving this logical problem?

exampleDEMO The code above the link demonstrates how to control input forms based on a button group selection. In this scenario, when a value is inputted on the 'First' Button's side, all input forms with the same name as the button group ...

Import data from an external JSON file

I'm having trouble retrieving JSON data from an external file. Oddly enough, it works perfectly when I include the JSON inline. However, after creating a file named test.json and transferring the JSON content into it, I can't seem to access the i ...

Proceed with another ajax request only when the previous one has been successfully completed and loaded

While scrolling down and loading content into my page, I am facing an issue. The ajax executions load too quickly, causing the subsequent calls to not receive correct information from the first ajax call that is loaded into the DOM. How can I ensure that ...

Display popup when hovering over an li element, but only after one second of hovering over it

My goal is to display an inner div when hovering over an li element. I have implemented a fadeIn and fadeOut effect, but the issue is that if I quickly hover over all li elements, the fadeIn effect triggers for all of them. Ideally, the inner div should on ...

Angular is able to select an element from a specified array

I'm currently struggling with using Angular to manipulate a TMDB API. I am having difficulty retrieving an item from an array. Can someone provide assistance? Here is the response that the array returns: { "id": 423108, "results ...