Using AXIOS and VueJs to visually represent data with a two-dimensional array

I'm new to vuejs and I want to display my data response in a two-dimensional table.

This is the data I have:

[{
  "origine": "",
  "nb": 15
}, {
  "origine": "?",
  "nb": 18
}, {
  "origine": "?L",
  "nb": 1
}, {
  "origine": "G",
  "nb": 298
}, {
  "origine": "I",
  "nb": 51
}, {
  "origine": "L",
  "nb": 1735
}, {
  "origine": "L?",
  "nb": 1
}, {
  "origine": "O",
  "nb": 4
}]

After mapping, I would like the data structure to be either of these formats:

[
  ['', 15],
  ['?', 18],
  ['?L', 1],
  ['G', 298],
  ['I', 51],
  ['L', 1735],
  ['L?', 1],
  ['O', 4]
]

Or like this :

[
  '': 15,
  '?': 18,
  '?L': 1, 
  'G': 298, 
  'I': 51, 
  'L': 1735, 
  'L?': 1, 
  'O': 4
]

Here is what I've tried so far:

getResultcivitas(localisation) {
  axios
    .get('../api/civitasorigine/' + this.searchInputcivitas)
    .then(response => {this.origines = response.data.map (x => x.origine)})
}

I found the following code snippet but unsure how to apply it in my axios query.

map = origines.map(obj => {
  var rObj = {};
  rObj[obj.origine] = obj.nb;
  return rObj;
});

Answer №1

To retrieve the desired data, simply use the map function and return an array with item.origine and item.nb

let information = [{
  "origine": "",
  "nb": 15
}, {
  "origine": "?",
  "nb": 18
}, {
  "origine": "?L",
  "nb": 1
}, {
  "origine": "G",
  "nb": 298
}, {
  "origine": "I",
  "nb": 51
}, {
  "origine": "L",
  "nb": 1735
}, {
  "origine": "L?",
  "nb": 1
}, {
  "origine": "O",
  "nb": 4
}]

let mappedInformation = information.map(item => {
  return [item.origine, item.nb];
})

console.log(mappedInformation)

Answer №2

You possess all the solutions, they just need to be integrated:

fetch('../api/civitasorigine/' + this.searchInputcivitas)
  .then(response => response.json())
  .then(data => {
    this.origins = data.map(obj => {
      const newObject = {};
      newObject[obj.origin] = obj.nb;
      return newObject;
    })
  })

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

Exporting two functions in JavaScript

Currently utilizing React, Redux, and experimenting with Material-UI integration. The example codes provided by Redux and Material-UI libraries include an 'export' statement at the end. Redux: export default connect(mapStateToProps, actions)(my ...

`Manipulating the image source attribute upon clicking`

I need help changing the attr: { src: ...} binding of an img element when a different image is clicked. Here is an example: $(document).ready(function () { var viewModel = { list: ko.observableArray(), showRenderTimes: ...

Discover the method for accessing a CSS Variable declared within the `:root` selector from within a nested React functional component

Motivation My main aim is to establish CSS as the primary source for color definitions. I am hesitant to duplicate these values into JavaScript variables as it would require updating code in two separate locations if any changes were made to a specific co ...

Consider developing a custom plugin that automatically loads all components within your nuxt.js application

I have been attempting to load and define all my components globally in nuxt.js. However, I encounter an error when trying to load the *.vue file: "Cannot find module ....". Despite the fact that the file exists in the specified location... Below is the ...

Searching for the perfect jQuery regex to validate date formats

In my application, there is an input box that allows users to enter a string date like "today" or "tomorrow". However, I am facing a new challenge now - dates such as "3 march" or "8 january." The input box includes a dropdown menu feature where users can ...

Node.js has no trouble loading HTML files, however, it seems to encounter issues when trying to

Having a bit of trouble with my JavaScript skills as I try to load my index.html file (seems like it should be the 5th easiest thing to do in JavaScript). Let me get straight to the point; when I manually open my index.html, it loads perfectly WITH the CS ...

Guide the user to a specific website and replicate the user's action of pressing the down arrow or clicking a button three consecutive times

Currently, I am facing an issue with a WordPress slider that lacks anchors for linking to specific sections. I am wondering if there is a way to direct a user to a URL and simulate the action of pressing the down arrow or clicking a button multiple times ...

Struggling with creating and exporting TailwindCSS and NextJS

I'm encountering challenges in constructing and exporting my TailwindCSS and NextJS project. While everything works smoothly when running `npm run dev` with all Tailwind classes functioning properly, I face an issue when executing `npm run build && np ...

Error: Unable to access properties of an undefined value (trying to read 'type') within Redux Toolkit

Looking for help with this error message. When trying to push the book object into the state array, I encounter an error. Folder structure https://i.stack.imgur.com/9RbEJ.png Snippet from BookSlice import { createSlice } from "@reduxjs/toolkit" const ...

Multi-object retrieval feature in Material Dialog

I am encountering an issue with Material Dialog when confirming the action to "remove row" in a table. Initially, removing from the dialog works fine and deletes a row. However, after closing the dialog and re-calling it for the second time, the removal ac ...

The latest alpha version of Angular2 Material Design (alpha.9-3) encountered a "404 not found" error when trying to access @angular

After carefully following the steps outlined in the angular material2 Getting Started guide to install @angular/material, I made updates to package.json, app.module, and systemjs.config using Atom. Specifically, I added the line '@angular/material&apo ...

Searching for root words in elasticsearch

After successfully implementing stemming for elasticsearch, I noticed that my searches for "code" also bring up results for "codes" and "coding," which is great. However, I encountered a problem when using the "must_not" field in my queries. Including "co ...

Checking parameters from two forms that are both associated with the same model in Rails

Recently, a new feature was added to the system - a phone and sim checker. Users are required to input their phone number into the first form. If the phone number is found in the database, a message indicating this is displayed. Otherwise, the form switche ...

Implementing the sticky positioning property to keep a div container fixed at the bottom of the page

As Bootstrap 4 no longer supports .affix, I had to look for an alternative solution to keep a box fixed. I needed a div container to remain fixed as you scroll to it. My current workaround is using: .fixedcard{ position: sticky; top:75%; } However, th ...

Developing a tool for switching between languages in an internationalization application

I have been exploring the implementation of Lingui(i18n) in apps. All set up, but I'm interested in adding a language switcher to enable users to change between language catalogs on my app. Here's my index.js file: import React, { useEffect } fr ...

Having issues with your network while installing packages via Yarn over a proxy?

There seems to be an issue with the proxy settings at my workplace. I have successfully added the proxy configuration and can run yarn commands like serve. However, a new problem has arisen - I am unable to npm install or use yarn install. What steps shou ...

Do you have an index.d.ts file available for canonical-json?

I am currently working on creating an index.d.ts file specifically for canonical-json. Below is my attempted code: declare module 'canonical-json' { export function stringify(s: any): string; } I have also experimented with the following sn ...

Best practices for including jQuery in ASP.NET (or any other external JavaScript libraries)

Can you explain the distinctions among these three code samples below? Is there a preferred method over the others, and if so, why? 1.Page.ClientScript.RegisterClientScriptInclude(typeof(demo), "jQuery", Re ...

Problem with fetching Grails

I have a table nested within an anchor tag. The table is centered on the page, with each row containing an image. When the page loads, I noticed the following: a) The table initially appears at the top-left corner of the screen. b) As there are multiple v ...

Having difficulties testing the functionality of redux-form

I am struggling with testing react components that use redux-form. My integration tests are failing, indicating that I have not set up the tests correctly. There seems to be a consensus in discussions on this issue, both here and on GitHub. Any assistance ...