简体   繁体   English

基于定义值的Javascript订单对象属性

[英]Javascript order object properties based on defined value

I have a two pieces of data I am working with to ultimately generate an HTML table. 我有两个数据要用来最终生成一个HTML表。

Data: This is the core data that would come from a database call. 数据:这是来自数据库调用的核心数据。

UserColumns: This defines which order the table columns will be in. The fieldSource in this object will align with the key names in the data object. UserColumns:定义表列将采用的fieldSource 。此对象中的fieldSource将与data对象中的键名对齐。

// Define the core data we will be working with. Assumed this comes from a database,
var data = [{
    FirstName: 'Joe',
    LastName: 'Jones',
    Age: 21,
    Location: 'Arizona',
    Color: 'Red', // Not defined in the column structure, therefore is not included in output
    Order: 1
  },
  {
    FirstName: 'Tim',
    LastName: 'Bob',
    Age: 25,
    Location: 'California',
    Color: 'Blue',
    Order: 3
  },
  {
    FirstName: 'Sally',
    LastName: 'Smith',
    Age: 29,
    Location: 'Florida',
    Color: 'Green',
    Order: 2
  }
];

// Defines the columns the user wants to include in their table output as well as their order
var userColumns = [{
    FieldID: 1,
    FieldSource: 'FirstName',
    FieldName: 'First Name',
    Order: 2
  },
  {
    FieldID: 2,
    FieldSource: 'LastName',
    FieldName: 'Last Name',
    Order: 1
  },
  {
    FieldID: 3,
    FieldSource: 'Age',
    FieldName: 'Age',
    Order: 4
  },
  {
    FieldID: 4,
    FieldSource: 'Location',
    FieldName: 'Location',
    Order: 3
  }
];

/*
    Loop over the data and order the content in the desired column order.
*/
function generateTableRows() {

  let output = [];

  for (var j = 0; j < data.length; j++) {
    // Need to re-order the object keys based on the "User Columns".
    // This also needs to ensure the the table rows are in the correct order based on the "Order" property
    console.log(data[j]);
  }

}

In the example code above, the keys in data need to be re-ordered to match the order defined in the userColumns object. 在上面的示例代码中, datakeys需要重新排序以匹配userColumns对象中定义的顺序。

End goal here is that the user will see the table of data in their desired view (columns in the defined order). 最终目标是使用户可以在所需视图中查看数据表(按定义顺序的列)。

How can I go about putting the keys in a specific order and will they remain that way in the object or can they change? 我如何才能按特定顺序放置密钥,它们会以这种方式保留在对象中还是可以更改?

I need to essentially take an object of data and then output re-arrange it so that its in the user defined order when it comes time to render a table. 实际上,我需要获取一个数据对象,然后将其输出重新排列,以便在呈现表时按用户定义的顺序进行排列。

Here is a fiddle of the full task at hand I am trying to workout: https://jsfiddle.net/os48s1ne/ 这是我正在尝试执行的全部任务的小提琴: https : //jsfiddle.net/os48s1ne/

Sort the userColumns property, then loop through it and use the FieldSource properties to access the corresponding properties in the data. userColumns属性进行排序,然后遍历它,并使用FieldSource属性访问数据中的相应属性。

 /* Loop over the data and order the content in the desired column order. */ function generateTableRows() { userColumns.sort((a, b) => a.Order - b.Order); let output = data.map(d => userColumns.map(({ FieldSource, FieldName }) => `${FieldName} = ${d[FieldSource]}`).join(', ')); console.log(output); } // Define the core data we will be working with. Assumed this comes from a database, var data = [{ FirstName: 'Joe', LastName: 'Jones', Age: 21, Location: 'Arizona', Color: 'Red', // Not defined in the column structure, therefore is not included in output Order: 1 }, { FirstName: 'Tim', LastName: 'Bob', Age: 25, Location: 'California', Color: 'Blue', Order: 3 }, { FirstName: 'Sally', LastName: 'Smith', Age: 29, Location: 'Florida', Color: 'Green', Order: 2 } ]; // Defines the columns the user wants to include in their table output as well as their order var userColumns = [{ FieldID: 1, FieldSource: 'FirstName', FieldName: 'First Name', Order: 2 }, { FieldID: 2, FieldSource: 'LastName', FieldName: 'Last Name', Order: 1 }, { FieldID: 3, FieldSource: 'Age', FieldName: 'Age', Order: 4 }, { FieldID: 4, FieldSource: 'Location', FieldName: 'Location', Order: 3 } ]; generateTableRows(); 

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

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