1

What I'm trying to do is extract just the data from the JSON and not the headers(for example, getting 1 but not ID or getting foo and not Name)

[{ID = 1, Name = "foo", Email = "foo@foo.com"},
{ID = 2, Name = "bar", Email = "bar@bar.com"}]

The reason that I just want the data and not the headers is that the data could by dynamic. In one call the JSON returned could have 100 fields per object or 2 fields per object on the next call. Which is why, in the below example, I only have a string in my interface, because I have no clue what kind of data could be passed.

Here is my typescript that I'm trying to get to interpret the data

import { Component } from '@angular/core';
import { Http } from '@angular/http';

@Component({
    selector: 'fetchdata',
    template: require('./fetchdata.component.html')
})
export class FetchDataComponent {
    public rowData: RowInfo[];

    constructor(http: Http) {

        http.get('/api/SampleData/DatatableData').subscribe(result => {
            //This is where the magic should happen
            //This currently does not work
            var dataCarrier = result.toString();
            JSON.parse(dataCarrier).forEach(item => {
                this.rowData = item.name;
            });
        });
    }
}

interface RowInfo {
    rowData: string;
}

How do I go about breaking up the JSON data in the http.get into just its pieces to pass through to the interface while differentiating between the different rows that could be in the same object?

Zach
  • 640
  • 1
  • 8
  • 20

1 Answers1

3

The ES6 way: This will give you an array consisting of 1 array per object that you originally had. Each of the sub-arrays will only be the values of those objects.

JSON.parse(dataCarrier).map(Object.values)

So in your example it would result in:

[{ID = 1, Name = "foo", Email = "foo@foo.com"},
{ID = 2, Name = "bar", Email = "bar@bar.com"}]

// =>

[[1, "foo", "foo@foo.com"], [2, "bar", "bar@bar.com"]]

For more info on Object.values: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Object/values

If you can't use ES6, you can do it with ES5 and Object.keys. The code will be more verbose, but it gets the job done:

JSON.parse(dataCarrier).map(function(obj) {
    return Object.keys(obj).map(function (key) {
        return obj[key];
    });
});

*Adapted from How to get all properties values of a Javascript Object (without knowing the keys)?

shotor
  • 763
  • 6
  • 17
  • When I try to use this code it throws an error on Object.values. It says "property 'values' does not exist on the type 'ObjectConstructor' – Zach Jul 06 '17 at 15:35
  • The code should run fine: https://jsfiddle.net/vdvjj72d/ If you're using `TypeScript` you can change your target to `ES6`: `target: 'es6'` in `tsconfig.json`. If that's not an option, let me know and I'll post the ES5 version, which will be a bit longer. Or if you're already using `lodash` or `underscore` you can replace `Object.values` with `_.values`. – shotor Jul 06 '17 at 15:43
  • It may not be an option. The project is a .NET Core with Angular project and it made a few tsconfig files. If you could post the es5 version as well I would appreciate it – Zach Jul 06 '17 at 15:59
  • 1
    @Joris, added `ES5` version and here's the updated fiddle: https://jsfiddle.net/vdvjj72d/1/ – shotor Jul 06 '17 at 16:58