John Theoden
John Theoden

Reputation: 325

Get type name from json data

I have object that have additional arrays inside and it looks like this:

[![enter image description here][1]][1]

As you can see, each array is some sort of "Type" (Activity,Connection,Object etc.) What I need is to make list of names from all arrays and all types

So, to make complete list of names from all elements in Type arrays.

So far, my code in component regarding this object is this:

    ngOnInit() {

      this.codeService.get().subscribe(
              response => { this.handleSuccess( response ); },
              error => { console.error( error ); });

  }


  handleSuccess( oTypes ) {
      this.oTypes = oTypes } 

}

...Where for oTypes i get object with this arrays from this picture.

Upvotes: 0

Views: 454

Answers (1)

danopz
danopz

Reputation: 3408

I just iterated over it a bit but looks like this is what you want to achieve.

var oTypes = {
    aType: [{name: 'Name1'}, {name: 'Name2'}],
    bType: [{name: 'N3'}, {name: 'N4'}]
};

var names = [];
for (var t in oTypes) {
    if (oTypes.hasOwnProperty(t)) {
        oTypes[t].forEach(function(v){
            names.push(v.name);
        });
    }
}

// ["Name1", "Name2", "N3", "N4"]
console.log(names);

Upvotes: 2

Related Questions