I have an array of objects with an "asymetric" structure where keys are not the same and there are some nested object. I need loop through this:
obj = [{
"d14042018": {
"date": "14-04-2018",
"value": 5
},
"d02042018": {
"date": "02-04-2018",
"value": 10
},
"name": "my name"
},
{
"d14042018": {
"date": "14-04-2018",
"value": 15
},
"d02042018": {
"date": "02-04-2018",
"value": 25
},
"name": "my second name"
}]
what i need is to return a structure like this
first row = my name 5 10
second row = my second name 15 25
I tried a for in a custom Pipe...
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'keys'
})
export class KeysPipe implements PipeTransform {
transform(value: any, args?: any): any {
let keys: string;
if (value) {
for (var prop in value) {
if(typeof value[prop] != 'object') {
keys = value[prop];
} else {
for (var subprop in prop) {
keys = prop.subprop;
}
}
}
}
return keys;
}
}
but id doesn't work...
can someone help?
Object.keys()then you have something you can iterate over. If the structure stays the same you can use the key index to gather thevalueand the next key to get the second value, and the third key (name) to get the name.