Listing data horizontally(in a table) instead of vertically

mhrvatin

I have an object with an array and a string:

{
    "result": [
    ],
    "text": "success
}

"result" contains four arrays with twelve objects

"result": [
    [
        {
        // data
        },
        // eleven more objects
    ],
    // three more arrays
]

The entire object is from a database which contains data about yearly income. In each of the twelve objects(one for each month) there's data about gross income, net income, expenses, savings and so on. Each array(direct child of "result"-array) is an array for a year.

What I'm trying to accomplish is listing the data horizontally, with each month as a column and each row has a "label" as a legend as to explain what the row contains.

This is my desired result. Notice how the the chart displays that the pay is increasing each month: correct

What I'm getting. February's gross is put under january's net, and so on: correct

I've tried nesting loops but I'm having a hard time breaking off to a new row. The first row(gross) spans twelve columns and the correct data is entered in each cell, but on the first row I get 12*6 cells, second row 12*5, third row 12*4 and so on. On top of this I need to loop through four years(each array in "result"-array).

This is what I use to build the table(s), and it works great: http://jsfiddle.net/w3mktubx/

This is the entire code for the table-looping: http://jsfiddle.net/v4vo6zot/1/

I've tried Object.keys() but wasn't successful on that part either. I know I could just make like 100+ lines of code and make it all but there should be a solution for this using loops. I'd also prefer to make the tables using JS since the table should only be created once the user navigates to that particular page.

The data in my month-objects is as follows. Doing it from memory since I'm at work right now, but it should be correct:

{
    "id": "0",
    "year": "2011",
    "month": "january",
    "gross": "10000",
    "net": "8000",
    "expenses": "2000",
    "afterExpenses": "6000",
    "savings": "4000",
    "moneyToSpend": "2000"
}

So my question is: is there an easy solution to this? If so, how do I do it?

mhrvatin

I solved it by adding some if and else if-cases, even though I really didn't want to use that.

Fiddle with comments.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Listing columns on a Slick table

How do I echo out table <tr> horizontally instead of vertically with while-loop?

Display data from database vertically in table

Listing grouped data in table rows

Listing of data in many to many table relations using .net mvc 5

ListView not listing data

Listing nested JSON data to table view

Read scraped data vertically out of table instead horizontally Python

Use data as arrays instead of table for Vega Lite

Random data listing and grouping

ggplot how to "read" data vertically instead of horizontally in R

Create data.table listing values of one variable grouped by another variable

Data Table input type as text instead of search

Horizontally and Vertically scrollable table view with data from json

Retrieving data from unnormalized vertically ordered table

PHP Table Data Show Results Vertically not Horizontally

How to split data vertically instead of horizontally?

Html Table : td data to be center. vertically and horizontally

How to listing equal rows of database table data?

Listing data with join in SQL

Listing common data in multiple tables belonging to a category table with MySQL

Listing Json data with JavaScript

display json data dynamically from any api in table by adding data in table vertically or convert row data to column data

Listing Json data with Python

Excel: Dependant Data Validation Listing values from table with criteria without Macro

How to merge data vertically so there are multiple lines instead of multiple columns

Data.table instead of dplyr

paste command puts data from csv files vertically line by line instead of horizontally next to each other

Directory Listing data Source