Convert an array containing properties into a vertical list

Dealing with a graphics library has presented a challenge as it struggles to handle the data format returned by the API I am utilizing. The issue lies in the fact that the chart lib does not support the structure of data from the api.

API response

const data = [
  {
      "house01": {
        "free": 6
      },
      "house02": {
        "free": 2
      },
      "house03": {
        "free": 1
      },
  }
]

Expected (UPDATED)

const data = [
          {
            "label": "house01"
            "free": 6
          },
          {
            "label": "house02"
            "free": 2
          },
          {
            "label": "house03"
            "free": 1
          },
    ]

Answer №1

Are you in need of this information?

const dataset = [
  {
    home01: {
      available: 6
    },
    home02: {
      available: 2
    },
    home03: {
      available: 1
    }
  }
];

const newData = Object.entries(dataset[0]).map(d => ({
  name: d[0],
  available: d[1].available
}));

console.log(newData)

const dataset2 = {
  home01: { available: 6 },
  home02: { available: 2 },
  home03: { available: 1 }
};

const newData2 = Object.entries(dataset2).map(d => ({
  name: d[0],
  available: d[1].available
}));

console.log(newData2);

Answer №2

const houses = [{
  "house01": {
    "available": 6
  },
  "house02": {
    "available": 2
  },
  "house03": {
    "available": 1
  },
}]
resultList = [];
tempObj = {};
Object.entries(houses[0]).forEach(item => {
  tempObj.houseName = item[0];
  Object.entries(item[1]).forEach(detail => {
    tempObj[detail[0]] = detail[1];
  })
  resultList.push(tempObj);
});
console.log(resultList);

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

Whenever attempting to choose a rating, the MUI dialog continuously refreshes and resets the selected rating

I'm facing an issue with my MUI dialog where it keeps refreshing and resetting the selected rating every time I try to rate a movie. Any assistance on this matter would be highly appreciated. The RateWatchMovieDialog component is designed to display ...

Interactive front end design for decision trees

On the front end, I aim to enable users to influence the outcome of a Decision Tree based on their selections. For my Django-React App, I have adopted the style and tree example from the codeplayer. You can find it here: I am tasked with creating an unor ...

Tally of number series in an array

If my array looks like [0, 2, 4, 10, 10, 10, 10, 2, 5, 3, 2, 10, 10, 5, 7, 4, 10, 10, 10, 10] How do I determine the number of times a sequence of 10's occurs with at least 3 repetitions. In this example, the output would be 2 because there are 2 s ...

Discovering the minimum and maximum Node.js versions that my Node.js application can support according to its dependencies

If I have 10 developer dependencies and 10 production dependencies, how can I decide on the minimum and maximum node versions required for a user to download or clone my node application and run it smoothly? How can I automate this process by analyzing all ...

Identifying duplicate values in an array of objects using JavaScript

I am facing an issue with my array that collects data from a spreadsheet into studentCCAList. Sometimes, a student may have multiple subjects. For example, Name: Joseph Subject: English Name: Peter Math Name: Joseph Subject: Science My concern i ...

Execute HTML code within a text field

Is it possible to run html code with javascript inside a text box, similar to w3schools.com? I am working solely with html and javascript. Thank you. For example, I have two text areas - one for inserting html, clicking a button to run the code, and displ ...

Currently, my focus is on creating a robust slash command handler for discord.js version 13

I've been attempting to update my command handler to support slash commands, but I keep encountering an Invalid Form Body error when trying to activate the bot. I'm hesitant to switch handlers since I use this one for all my bots, but I can&apos ...

Struggling with collaborating with an assistant in handlebars and express operations

When attempting to utilize helpers, an error arises: ReferenceError: a is not defined The goal is to display home.hbs located under the views directory. The file contains: <li class="{{#if_eq title "Home"}}active{{/if_eq}}"> <a href="/">H ...

Pattern for identifying JavaScript import declarations

My task involves using node to read the contents of a file. Specifically, I am looking to identify and extract a particular import statement within the file that includes the "foo-bar" package. The goal is to match only the line or lines that contain the ...

Potential 'undefined' object detected in Vuex mutation using TypeScript

Currently, I am diving into learning Vue.js alongside Vuex and TypeScript. While working on my application, I encountered an error stating "Object is possibly 'undefined'" within the Vuex Store. The error specifically arises in the "newCard" mut ...

GridView in ASP.NET

I am currently working with a text box that is embedded within a gridview. My goal is to retrieve the ID of the textbox using JavaScript. However, when I try using '<%= txtNewQty.ClientID %>', it results in a compilation error. ...

The function for executing the specific command is not recognized and is resulting in a TypeError: client.commands

I'm having an issue with the code below and need a solution. Please help. Error : TypeError: client.commands.get(…).execute is not a function I am encountering difficulty with this specific command in my code: client.command ...

Alpine declined the action of toggling a list with x-show

My goal is to create a toggleable list of items using Alpine & x-show. I want the list to be visible when the page loads and allow the user to toggle it as needed. Although the button works correctly and the JavaScript is set up properly, the list itself d ...

*ngIf doesn't seem to be functioning as expected compared to other *ngIf directives

Encountering a peculiar issue with my *ngIf related to the isAdmin variable which determines whether the list of users in userList should be displayed or not. Strangely, it seems to behave differently compared to other *ngIf statements within the same comp ...

Regular Expression to Replace Characters Not Matching

I am struggling with a coding issue that involves manipulating a string. The original string I have is "Hello This is world This". Here is the code snippet I have tried: var patt = 'Hello This is world This' var res = patt.constructor; alert( ...

Guide on utilizing Vercel KV for storing and fetching posts from an API

Looking to optimize your API by utilizing Vercel KV for storing and retrieving posts? If you have a basic node.js express API that pulls random posts from MongoDB, the process of integrating Vercel KV can enhance performance. Initially, the API will resp ...

"Exploring the possibilities of customizing Material UI tabs and implementing a tabs scroller

I'm currently trying to customize the appearance of these MUI tabs, specifically the tab color and bottom border color. Despite my attempts using makeStyles and other methods, I haven't been able to achieve the desired result. https://i.sstatic.n ...

Retrieve Data from an Object using a Different Value

It sounds silly, but I'm struggling with this. I'm working with a basic JS object: var objSyntax = [ {"a": "1"}, {"b": "2" }, {"c": "3"} ]; My goal is to retrieve the value associated with key 'a&apos ...

What could be the reason for the malfunction of my personalized validation function?

Struggling with incorporating custom validation functions in Sails models. Everything runs smoothly without the custom function. However, once I add the function, everything comes to a halt. No data is saved to the database, the callback isn't trigg ...

What is the best way to display an image path and add it to the Ajax success function in a CodeIgniter application?

I am struggling to display my image path correctly using append and a variable to store the value. However, whenever I try, it results in an error. Let me provide you with the code snippet: <script type="text/javascript"> $(document).ready(funct ...