react js添加计数json元素

zd287kbt  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(355)

所有人!我使用react,希望json输出count“employees”,如下所示:在这里输入图像描述

[
{
    "id": 1,
    "departmentName": "Development",
    "employees": [
        {
            "id": 1,
            "name": "Max",
            "departmentId": 1,
            "salary": 20000.0,
            "city": "berlin",
            "street": "First st.",
            "bankName": "bank",
            "cardNumber": "de12345678912"
        },
        {
            "id": 2,
            "name": "Sam",
            "departmentId": 1,
            "salary": 20000.0,
            "city": "new york",
            "street": "Second st.",
            "bankName": null,
            "cardNumber": null
        }
    ]
},

对于呈现json,我使用datatable

<DataTable value={this.state.departments} paginator={true} rows="25" selectionMode="single"
                           selection={this.state.selectedDepartment}
                           onSelectionChange={e => this.setState({selectedDepartment: e.value})}>
                    <Column field="id" header="ID"/>
                    <Column field="departmentName" header="Department"/>
                </DataTable>

如何为count employees添加字段?

byqmnocz

byqmnocz1#

如果要使用此datatable(即使它没有column field属性),则可以为其指定selector属性:

<Column
  selector={(row) => row.employees.length}
  name="Employees"
/>
bq3bfh9z

bq3bfh9z2#

实际上这很容易

<Column field="employees.length" header="Employees"/>

相关问题