微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用字符串点表示法访问嵌套对象

如何解决如何使用字符串点表示法访问嵌套对象

如何使用字符串点表示法访问嵌套对象

我正在创建一个用于响应的表,

这个问题其实可以通过设置raw:trueSequelize来解决,但是我不想破坏对象结构。

我通过 columns.accessor 指定了对象键,但是当我想使用点表示法访问嵌套对象时,它返回一个未定义的值。

如何解决这个问题?

const orderDetails = [
    {
        "id": 3,"userID": 15,"user": {
            "id": 15,"firstName": "jhon","lastName": "mayer","email": "mayer@gmail.com","createdAt": "2021-03-12T01:49:35.000Z","updatedAt": "2021-03-12T01:49:35.000Z"
        }
    },{
        "id": 2,"userID": 14,"user": {
            "id": 14,"firstName": "Jhon","lastName": "Doe","email": "doe@gmail.com","createdAt": "2021-03-07T08:23:16.000Z","updatedAt": "2021-03-13T03:51:16.000Z"
        }
    }
];

const columns = [
  {title: 'User ID',accessor: 'userID'},{title: 'Email',accessor: 'user.email'} // user.email undefined
];
          
// mapping to React table
const rows = orderDetails.map(d => {
  return columns.map(col => d[col.accessor]);
})

console.log(rows)
// [ [ 15,undefined ],[ 14,undefined ] ]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。