Skip to content
Advertisement

JS – How to concat a dynamic variable name using two objects?

I have this first array:

JavaScript

And a second array fill with some data.

props.data.data has the following array:

JavaScript

I’m trying to mount a HTML table in a react component using map functions, but due to the dynamic column field, the value returns undefined and i don’t know why.

import React from ‘react’;

JavaScript

So, how can i concat the dynamic value from column array to get the right value?

Advertisement

Answer

The issue is that your field in the column definition is telling you the path in the data object. The simplest solution would be to just drop the ‘attributes.’ part from the definition and use row.attributes[column.field] (this of course assumes you always need to access fields from ‘attributes’. However, if you need to access any nested property or don’t control the columns definition, you can create an accessor method to parse the path and recurse down to get the value.

Something like this should work:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement