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

Unexpected output from nested loop implementation

Having some arrays, I am now trying to iterate through all tab names and exclude the values present in the exclusion list. json1 ={ "sku Brand": "abc", "strngth": "ALL", "area ...

Tips for extracting a URL from a specific section of a JSON object

I am working with a JavaScript variable like this- var uri = "https:\/\/maps.googleapis.com\/maps\/api\/staticmap?size=100x100&zoom=11&center=22.816667,89.55"; I want to convert it to look like this- var uri = "https://m ...

The function cannot be accessed during the unit test

I have just created a new project in VueJS and incorporated TypeScript into it. Below is my component along with some testing methods: <template> <div></div> </template> <script lang="ts"> import { Component, Vue } from ...

Extracting Ajax data - iterating through each piece of information

I am facing an issue with my ajax code. I have a collection of checkboxes, where the goal is to insert the data from the selected checkboxes into the database. When I choose just one checkbox, the insertion works fine. However, if I select multiple checkb ...

Exploring the history and present state of Vue lifecycle hooks

Is there a way to access previous and current data in the updated lifecycle hook in Vue, similar to React? I want to be able to scroll a list of elements to the very bottom, but for this I need: The already rendered updated DOM (to calculate the scroll) ...

Using jQuery to dynamically insert a table row with JSON data into dropdown menus

I'm working on a web form that includes a table where users can add rows. The first row of the table has dependent dropdowns populated with JSON data from an external file. Check out the code snippet below: // Function to add a new row $(function(){ ...

Trouble with browser caching when utilizing code-splitting for production? (Encountering difficulties fetching dynamically imported module)

Using Vue + Vite for my web application, I have implemented code-splitting in the router file for most routes. An example of this is: { path: "settings", name: "settings", component: () => import("../views/SettingsView.vue&q ...

Methods for applying responsive design to react modals in React.js

I am looking to make my modal responsive across different media types. In my React JS project, I have implemented custom styles using the following code: import Modal from 'react-modal'; const customStyles = { content : { top ...

transferring scoped model information to the controller

When using AngularJS, my view is structured like this: <div class="sli1" ng-init="values=[10,20,30,40,50]" <div class="sli2" ng-init="values2=[10,20,30,40,50]" I am attempting to send the initial data models back to the controller for retrieva ...

Issue with dynamically filling a form field using ValidityState

I have been utilizing the ValidityState interface for client-side form validation, but I've encountered an issue. Whenever my form is populated programmatically, such as after making a call to a third-party API, the tooLong state always returns false, ...

Is there a way to remove a dynamically rendered component from a list?

Whenever I click a button, the same component is dynamically rendered on top of the list. But now, I need to implement a feature where users can delete any component from the list by clicking a cancel button associated with each component. Here's my ...

What is the best way to keep the calendar of a Datepicker always visible while still being able to select a date easily?

When I write my code, the calendar only appears when I press the TextBox. I attempted to place the datepicker in a <div>, but then I was unable to retrieve the selected date. @model Plotting.Models.CalendarModel @using (Html.BeginForm("Calendar", "H ...

Ways to verify if an array contains two identical object values?

I am in search of a way to determine whether my array contains duplicate object values or not. Let's say I have the following array of objects: const array = [ { id: "id1", quantity: 3, variation: "red", ...

Conflict in the naming and scoping of IE7 and IE8

While reviewing some code, I encountered a problem in Internet Explorer 7 and 8. Despite the constructor executing properly when stepped through, upon returning I received an error stating "Object does not support this property or method." How frustrating! ...

What is the best way to eliminate a blank array in JavaScript?

After countless hours of searching, I am reaching out for help with an issue that has me stumped. My Node.js application relies on JSON files for project configurations. Using the 'fs' module, I read the JSON file, parse it into a JavaScript obje ...

Is there a problem with Framer Motion exit and AnimatePresence in Next.js?

Why isn't my exit prop or AnimatePresence working as expected? This is my custom variant: const imgVariant = { initial: { opacity: 0, y: -100, }, animate: { opacity: 1, y: 0, transition: { type: " ...

What is the best approach for managing multiple socket rooms?

I have been working on developing a chat application that supports multiple chat rooms. After watching several tutorials, I have devised a way to handle this. const io = require("socket.io")(http); io.on("connection", (socket) => { ...

Adapting to more user inputs in accordance with the feedback received from the AJAX response

Two user inputs are being received: area time Utilizing this information, the available restaurants in the specified area during that particular time are displayed. In addition, all the cuisines offered by these restaurants are displayed as checkboxes. ...

Discovering the identity of an item

Assume I have some JavaScript code like this: Foo = { alpha: { Name: "Alpha", Description: "Ipso Lorem" }, bravo: { Name: "Bravo", Description: "Nanu, Nanu" }, delta: { Name: "Fly with me", Desc ...

Implementing image max-width and maintaining aspect ratios for responsiveness

Within the code snippet below and utilizing Bootstrap, a grid layout is defined with several .block components, each comprising an image and a title. The images are designed to be larger than the column size so that they can expand to full width for respon ...