question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to access nested object properties in columns

See original GitHub issue

Hello, I am passing an array of objects in data prop. Those objects in the array have some nested objects. I need to access the nested object properties. How can I do that? Demo data:

const data = [
{
    id: 1,
    person: {
      name: "Bla"
    }
  },
  {
    id: 2,
    person: {
      name: "Lol"
    }
  }
]

Expected column:

<MUIDataTable
title="Person List"
columns={
  [{
      name: 'id',
      label: 'ID',
      options: {
        filter: true,
        sort: true,
      }
    },
    {
      name: 'person.name',
      label:  'Person Name',
      options: {
        filter: true,
        sort: false,
      },
    },
  ]
}
data={history}
/>

So all I want is to show the person name in column. How can I do that? The person.name in above code doesn’t work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
raikusycommented, Apr 4, 2019

Hey thanks for all the response. My problem is solved.

function flattenObject(ob) {
  const toReturn = {};

  Object.keys(ob).map(i => {
    if (typeof ob[i] === 'object' && ob[i] !== null) {
      const flatObject = flattenObject(ob[i]);
      Object.keys(flatObject).map(x => {
        toReturn[`${i}.${x}`] = flatObject[x];
        return x;
      });
    } else {
      toReturn[i] = ob[i];
    }
    return i;
  });
  return toReturn;
}

I got data like:

{
  id: 2,
  "person.name": "Lol"
}
2reactions
MBCSDavecommented, Apr 3, 2019

I think the only way you have is to flatten your data structure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I render a nested object's property inside an array ...
Consider using ids and mapping to external object data in custom renderers instead e.g. const data = [{"Name": "Joe", "ObjectData": 123}] --> ...
Read more >
Accessing a nested object to display as a column i...
I want to display nested properties as columns, too. So where I normally have ThisItem.Property1 for one column, I'd like to do ThisItem....
Read more >
Nested object data (objects) - DataTables example
Nested object data (objects). DataTables has the ability to use data from almost any JSON data source through the use of the columns.data...
Read more >
Accessing Nested Objects in JavaScript - DEV Community ‍ ‍
Array reduce method is very powerful and it can be used to safely access nested objects. const getNestedObject = (nestedObj, pathArr) = ...
Read more >
Syntax for nested objects in DataWindow property ...
Nested objects include composite or related nested reports and child DataWindows associated with DropDownDataWindow columns. Related nested and composite ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found