javascript - 如何在 Javascript 中构造最终数组
问题描述
我有一个数组:
result = [
{ 101: {type: "A"},
table: "Employee",
column: "emp_id",
constraint: "unique"
},
{ 101: {type: "B"},
table: "Employee",
column: "emp_id",
constraint: "not_null"
},
{ 101: {type: "B"},
table: "Employee",
column: "name",
constraint: "unique"
},
{ 102: {type: "A"},
table: "Employee",
column: "emp_id",
constraint: "unique"
},
{ 102: {type: "B"},
table: "Employee",
column: "name",
constraint: "unique"
},
{ 103: {type: "B"},
table: "Employee",
column: "emp_id",
constraint: "unique"
},
];
并且列是动态的:
columns = [
{Header: "Table", accessor: "table"},
{Header: "Column", accessor: "column"},
{Header: id, accessor: id}
];
因此,如果有 5 个 id,例如 101、102、103、104 和 105,那么列将是:
columns = [
{Header: "Table", accessor: "table"},
{Header: "Column", accessor: "column"},
{Header: 101, accessor: 101},
{Header: 102, accessor: 102},
{Header: 103, accessor: 103},
{Header: 104, accessor: 104},
{Header: 105, accessor: 105},
];
我能够构造动态列标题但无法构造行。 我试图在 ReactTable 中显示这些数据,例如:
任何帮助,将不胜感激!
解决方案
鉴于我们需要按(表、列、约束)公共值进行分组,我们可以将其用作某个dataMap
对象的唯一字符串化键。该对象将相应地对每对值进行分组numberColumn
。
在进行映射迭代之后,我们将字符串化的键与其值连接在一起。我们将键解析回对象。
最后,我们可以按长度排序(基于图像)。
result = [
{ 101: {type: "A"},
table: "Employee",
column: "emp_id",
constraint: "unique"
},
{ 101: {type: "B"},
table: "Employee",
column: "emp_id",
constraint: "not_null"
},
{ 101: {type: "B"},
table: "Employee",
column: "name",
constraint: "unique"
},
{ 102: {type: "A"},
table: "Employee",
column: "emp_id",
constraint: "unique"
},
{ 102: {type: "B"},
table: "Employee",
column: "name",
constraint: "unique"
},
{ 103: {type: "B"},
table: "Employee",
column: "emp_id",
constraint: "unique"
},
];
const buildData = (result) => {
// dataMap will have unique keys to group by 'table', 'column', 'constraint'
const dataMap = {};
result.forEach(({ table, column, constraint, ...type }) => {
// we create a key with stringify
const key = JSON.stringify({table, column, constraint})
// type entries return will be lile [[ '101', { type: 'B' } ]]
const [number, typeObj] = Object.entries(type)[0]
const newType = {[number]: typeObj.type }
// if there is no type to that key yet we return the object, otherwise add the new one
dataMap[key] = !dataMap[key] ? newType : { ...dataMap[key], ...newType }
});
// here we join stringified values with numbered Columns
const data = Object.entries(dataMap).map(([ stringfiedCommonColumns, numberedColumns ]) => {
const commonColumns = JSON.parse(stringfiedCommonColumns)
return { ...commonColumns, ...numberedColumns }
})
// here we sort by length (it seems that it's in your interest)
const dataSorted = data.sort((a, b) => Object.keys(a).length < Object.keys(b).length ? 1 : -1)
return dataSorted
}
console.log(buildData(result))
推荐阅读
- android - Android Studio UI 文本(如文件名、选项等)在 MAC 中很小
- pandas - 如何通过在熊猫中添加或减去两个时间列来创建新列
- azure - 使用 UseDevelopmentStorage = true 运行时 Azure 函数抛出错误
- excel - 这是 Microsoft Excel 的数据可视化和数据操作问题
- python-3.x - 通过 Python3 调用 ThingsBoard REST API
- error-handling - caseMatch 测试不起作用:只有一列
- powershell - 更改现有脚本中的 DNS 查找
- python - ValueError: 为 '{{node max_pooling2d_16/MaxPool}} 从 1 中减去 2 导致的负维度大小
- powerbi - Power BI 报表与 Razor Pages 的集成
- python - 在Tensorflow中使用model_zoo的faster_rcnn_resnet101_v1_640x640_coco17_tpu-8时遇到错误==2.5