数组排序而不更改某些特定项目的顺序

时间:2019-02-13 05:30:46

标签: javascript

我想对这个数组进行排序,就像排名前四年的所有数组都排在最后,而不改变其他数组的顺序,应该不使用regex来完成它。 请注意,这种情况下的年数不是固定的,在其他迭代中,年数可能仅为2。

我试图对其进行排序并反转其工作方式,但这改变了其他人的顺序。

[ '2019',
  '2020',
  '2021',
  '2022',
  '2023',
  '_id',
  'Tender Ref Number',
  'Tender Number',
  'Additional Information',
  'Tender Type',
  'Country Exchange Rate',
  'Start Date',
  'End Date',
  'Submission Closing Date',
  'Result Date',
  'Country',
  'Region',
  'Distributer Name',
  'Franchise Name',
  'Account Name',
  'Local Account Name',
  'Win/Lost',
  'SAP Material Code',
  'ICC Code',
  'Quantity',
  'Description',
  'Standard Cost Per SAP Unit',
  'Tender Price Local',
  'Tender Price USD',
  'Total Sales',
  'Tender Currency',
  'quantity_distribution',
  'Approvers' ]

[ '_id',
  'Tender Ref Number',
  'Tender Number',
  'Additional Information',
  'Tender Type',
  'Country Exchange Rate',
  'Start Date',
  'End Date',
  'Submission Closing Date',
  'Result Date',
  'Country',
  'Region',
  'Distributer Name',
  'Franchise Name',
  'Account Name',
  'Local Account Name',
  'Win/Lost',
  'SAP Material Code',
  'ICC Code',
  'Quantity',
  'Description',
  'Standard Cost Per SAP Unit',
  'Tender Price Local',
  'Tender Price USD',
  'Total Sales',
  'Tender Currency',
  'quantity_distribution',
  'Approvers',
  '2019',
  '2020',
  '2021',
  '2022',
  '2023',]

4 个答案:

答案 0 :(得分:0)

尝试使用Array.spliceArray.filter来检查元素是否为年份(数字)和...扩展运算符:

var data = [ '2019', '2020', '2021', '2022', '2023', '_id', 'Tender Ref Number', 'Tender Number', 'Additional Information', 'Tender Type', 'Country Exchange Rate', 'Start Date', 'End Date', 'Submission Closing Date', 'Result Date', 'Country', 'Region', 'Distributer Name', 'Franchise Name', 'Account Name', 'Local Account Name', 'Win/Lost', 'SAP Material Code', 'ICC Code', 'Quantity', 'Description', 'Standard Cost Per SAP Unit', 'Tender Price Local', 'Tender Price USD', 'Total Sales', 'Tender Currency', 'quantity_distribution', 'Approvers' ];

let years = data.splice(0, data.filter((ele) => !isNaN(ele)).length);
data = [...data, ...years];
console.log(data);

答案 1 :(得分:0)

假设 n年年之后,下一项将是数组中的_id。您可以splice数组,直到_id,最后concat

let arr = [ '2019', '2020', '2021', '2022', '2023', '_id', 'Tender Ref Number', 'Tender Number', 'Additional Information', 'Tender Type', 'Country Exchange Rate', 'Start Date', 'End Date', 'Submission Closing Date', 'Result Date', 'Country', 'Region', 'Distributer Name', 'Franchise Name', 'Account Name', 'Local Account Name', 'Win/Lost', 'SAP Material Code', 'ICC Code', 'Quantity', 'Description', 'Standard Cost Per SAP Unit', 'Tender Price Local', 'Tender Price USD', 'Total Sales', 'Tender Currency', 'quantity_distribution', 'Approvers' ];
  
arr = arr.concat(arr.splice(0, arr.indexOf('_id')));
console.log(arr);

答案 2 :(得分:0)

let a = [ '2019',
  '2020',
  '2021',
  '2022',
  '2023',
  '_id',
  'Tender Ref Number',
  'Tender Number',
  'Additional Information',
  'Tender Type',
  'Country Exchange Rate',
  'Start Date',
  'End Date',
  'Submission Closing Date',
  'Result Date',
  'Country',
  'Region',
  'Distributer Name',
  'Franchise Name',
  'Account Name',
  'Local Account Name',
  'Win/Lost',
  'SAP Material Code',
  'ICC Code',
  'Quantity',
  'Description',
  'Standard Cost Per SAP Unit',
  'Tender Price Local',
  'Tender Price USD',
  'Total Sales',
  'Tender Currency',
  'quantity_distribution',
  'Approvers' ];
let years = a.filter(elem => !isNaN(Number(elem));
years = years.sort((a,b) => a-b);
let finalArr = a.filter(elem => isNaN(Number(elem));
finalArr.concat(years);

答案 3 :(得分:0)

如果年份可以在数组的任何位置,则可以通过以下方式使用reduce()

const input = [ 
  '2019', '2020', '2021', '2022', '2023', '_id', 'Tender Ref Number',
  'Tender Number', 'Additional Information', 'Tender Type',
  'Country Exchange Rate', 'Start Date', '2024', 'End Date',
  'Submission Closing Date', 'Result Date', 'Country', 'Region',
  'Distributer Name', 'Franchise Name', 'Account Name', 'Local Account Name',
  'Win/Lost', 'SAP Material Code', 'ICC Code', 'Quantity', 'Description',
  'Standard Cost Per SAP Unit', 'Tender Price Local', '2025',
  'Tender Price USD', 'Total Sales', 'Tender Currency',
  'quantity_distribution', 'Approvers'];

 let res = input.reduce(
     (acc, curr) => (isNaN(curr) ? acc[0].push(curr) : acc[1].push(curr), acc),
     [[],[]]
 );
 
 console.log(res[0].concat(res[1]));