I need to reach to the property: value in each drupal_internal__target_id: 2220, but i don't know how can i map to get that value. This is what i tried so far...
organismosId(){
// rel = data[0].relationships.organismo_auditado.data[0].meta.drupal_internal__target_id
// attr = included[0].attributes.drupal_internal__tid
this.buscadorService.getNodes()
.subscribe((data: InformesCounter) => {
this.informesNode = data.data;
const relationships = this.informesNode.map((data: { relationships: { organismo_auditado: { data: any; }; }; }) => data.relationships.organismo_auditado.data);
console.log(relationships);
const nodeRelationships = relationships.map((data: any) => data);
console.log(nodeRelationships);
})
}
And this is my JSON response of what i want to get:
The problem are the indexes like [0] and when i finally got there it returns undefined. Thanks
You can flat-map the overall data entries and internally map the data nodes inside the relationship metadata.
const run = () => {
const ids = data.flatMap(({ relationships: { organismo_auditado: { data } } }) =>
data.map(({ meta: { drupal_internal_target_id } }) =>
drupal_internal_target_id));
console.log(ids);
};
const data = [{
relationships: {
organismo_auditado: {
data: [{
id: '41ff8a06-c678-4db0-aa68-7887fd318345',
meta: {
drupal_internal_target_id: 2220
}
}]
}
}
}, {
relationships: {
organismo_auditado: {
data: [{
id: 'c677dcc2-b7b7-4bf9-8cbf-c8c6faf9aa6f',
meta: {
drupal_internal_target_id: 2221
}
}]
}
}
}];
run();
If you also want to include the ID, you can map objects:
const run = () => {
const ids = data.flatMap(({ relationships: { organismo_auditado: { data } } }) =>
data.map(({ id, meta: { drupal_internal_target_id } }) =>
({ id, drupal_internal_target_id })));
console.log(ids);
};
const data = [{
relationships: {
organismo_auditado: {
data: [{
id: '41ff8a06-c678-4db0-aa68-7887fd318345',
meta: {
drupal_internal_target_id: 2220
}
}]
}
}
}, {
relationships: {
organismo_auditado: {
data: [{
id: 'c677dcc2-b7b7-4bf9-8cbf-c8c6faf9aa6f',
meta: {
drupal_internal_target_id: 2221
}
}]
}
}
}];
run();