简体   繁体   English

如何根据定义的属性过滤和排序 object?

[英]How to filter and sort an object based on defined properties?

I have a table where I have a function that displays only the selected columns.我有一个表,其中有一个 function,它只显示选定的列。

I store the selected columns (table headings) in an array selectedTableHeaders .我将选定的列(表标题)存储在数组selectedTableHeaders中。

Now I want to make sure that I only display the selected columns in the table data.现在我想确保只显示表数据中的选定列。

So that means I want to create or filter a new array based on the properties stored in the selectedTableHeaders .所以这意味着我想根据存储在selectedTableHeaders中的属性创建或过滤一个新数组。

Furthermore, I want to make sure that the tableData is properly ordered, so if I for example disable table header 3 and then table header 6 and then enable 3 again, 3 is added later.此外,我想确保 tableData 的顺序正确,因此如果我禁用表 header 3,然后禁用表 header 6,然后再次启用 3,则稍后添加 3。 That means that I also have to reorder the tableData based on the table headers.这意味着我还必须根据表头对 tableData 重新排序。

How can I solve this?我该如何解决这个问题?

const selectedTableHeaders = [
    "table_header_1",
    "table_header_3",
    "table_header_5",
    "table_header_6"
]

tableData [
    {
        "rowData": {
            "table_header_1": "0",
            "table_header_2": "data 2",
            "table_header_3": "US",
            "table_header_4": "data 4",
            "table_header_5": "-",
            "table_header_6": "data 6"
        }
    },
    {
        "rowData": {
            "table_header_1": "0",
            "table_header_2": "test 2",
            "table_header_3": "GB",
            "table_header_4": "test 4",
            "table_header_5": "Y",
            "table_header_6": "test data 6"
        }
    },
    {
        "rowData": {
            "table_header_1": "0",
            "table_header_2": "test 2",
            "table_header_3": "DE",
            "table_header_4": 70000118,
            "table_header_5": "-",
            "table_header_6": "test table 6"
        }
    }
]

I have tried something like:我试过类似的东西:

this.tableData.forEach((tableItem) => {
        const newArray = Object.assign(...selectedTableHeaders.map(k => ({ [k]: tableItem[k] })));
})

But then I don't get the values in the newArray.但是后来我没有得到 newArray 中的值。 Is there a better way to handle this and get also the values of the properties in the new array?有没有更好的方法来处理这个问题并获取新数组中的属性值? So I want to create a new array with only the selected columns.所以我想创建一个只有选定列的新数组。

And how can I make sure that the table data is well-ordered, based on the table headings.以及如何根据表格标题确保表格数据井然有序。

So eg:所以例如:

If this is the order for the headings:如果这是标题的顺序:

"table_header_2",
"table_header_1",
"table_header_5",
"table_header_4"

That the rowData also becomes like this:那rowData也变成这样:

"rowData": {
    "table_header_2": "data 2 ",
    "table_header_1": "0",
    "table_header_5": "-",
    "table_header_4": "data 4",
}

You can simply achieve it by iterating the array object.您可以通过迭代数组 object 来简单地实现它。

Working Demo:工作演示:

 const selectedTableHeaders = [ "table_header_1", "table_header_3", "table_header_5", "table_header_6" ] const tableData = [ { "rowData": { "table_header_1": "0", "table_header_2": "data 2 ", "table_header_3": "US", "table_header_4": "data 4", "table_header_5": "-", "table_header_6": "data 6" } }, { "rowData": { "table_header_1": "0", "table_header_2": "test 2", "table_header_3": "GB", "table_header_4": "test 4", "table_header_5": "Y", "table_header_6": "test data 6" } }, { "rowData": { "table_header_1": "0", "table_header_2": "test 2", "table_header_3": "DE", "table_header_4": 70000118, "table_header_5": "-", "table_header_6": "test table 6" } } ]; const res = tableData.map((rowDataObj) => { Object.keys(rowDataObj.rowData).forEach((headerKey) => { if (.selectedTableHeaders.includes(headerKey)) { delete rowDataObj;rowData[headerKey] } }). return rowDataObj;rowData; }). console;log(res);

You could just map your fields, seen as there already in the order you want, and then get the values using the keys,你可以只是 map 你的领域,被视为已经按照你想要的顺序存在,然后使用键获取值,

reg.注册。

 const selectedTableHeaders = [ "table_header_1", "table_header_3", "table_header_5", "table_header_6", ]; const tableData = [ { rowData: { table_header_1: "0", table_header_2: "data 2 ", table_header_3: "US", table_header_4: "data 4", table_header_5: "-", table_header_6: "data 6", }, }, { rowData: { table_header_1: "0", table_header_2: "test 2", table_header_3: "GB", table_header_4: "test 4", table_header_5: "Y", table_header_6: "test data 6", }, }, { rowData: { table_header_1: "0", table_header_2: "test 2", table_header_3: "DE", table_header_4: 70000118, table_header_5: "-", table_header_6: "test table 6", }, }, ]; function filter(src, fields) { return src.map((row) => ({ rowData: Object.fromEntries( fields.map((m) => [m, row.rowData[m]])), })); } console.log(filter(tableData, selectedTableHeaders));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM