打字稿 - 重新排列特定对象

时间:2018-04-23 16:17:37

标签: javascript angular typescript object

我按字母顺序排列了对象。我想搬家"其他"最后在下拉列表中。有没有办法实现这个目标?



this.registrationMerchantService.getBusinessCategories().subscribe(response => {
  this.businessCategories = response;
  this.businessCategories.sort(function(a, b) {
    if (a.name < b.name) {
      return -1;
    }
    if (a.name > b.name) {
      return 1;
    }
    return 0;
  });
});
&#13;
&#13;
&#13;

Json对象

&#13;
&#13;
export const BUSINESS_CATEGORIES: BusinessCategory[] = [
  { id: 1, name: 'Food & Beverage' },
  { id: 2, name: 'Transportation & Travel' },
  { id: 3, name: 'Fashion' },
  { id: 4, name: 'Leisure' },
  { id: 5, name: 'Digital Content' },
  { id: 6, name: 'Health and Beauty' },
  { id: 7, name: 'Online Shopping' },
  { id: 8, name: 'Telecommunications' },
  { id: 9, name: 'Utilities' },
  { id: 10, name: 'Others' },
  { id: 11, name: 'General' },
  { id: 12, name: 'Insurance' },
  { id: 13, name: 'Services' },
  { id: 14, name: 'Appliances' },
  { id: 15, name: 'Entertainment' },
  { id: 16, name: 'Household Goods & Groceries' },
];
&#13;
&#13;
&#13;  当前结果: enter image description here

有任何想法移动&#34;其他&#34;最后在下拉列表中

2 个答案:

答案 0 :(得分:1)

您可以尝试对数组进行排序,过滤出“其他”,然后在最后添加它:

    var categories = [
      { id: 1, name: 'Food & Beverage' },
      { id: 2, name: 'Transportation & Travel' },
      { id: 3, name: 'Fashion' },
      { id: 4, name: 'Leisure' },
      { id: 5, name: 'Digital Content' },
      { id: 6, name: 'Health and Beauty' },
      { id: 7, name: 'Online Shopping' },
      { id: 8, name: 'Telecommunications' },
      { id: 9, name: 'Utilities' },
      { id: 10, name: 'Others' },
      { id: 11, name: 'General' },
      { id: 12, name: 'Insurance' },
      { id: 13, name: 'Services' },
      { id: 14, name: 'Appliances' },
      { id: 15, name: 'Entertainment' },
      { id: 16, name: 'Household Goods & Groceries' },
    ];
    const sorted =   categories.sort(function(a, b) {
      if (a.name < b.name) {
        return -1;
      }
      if (a.name > b.name) {
        return 1;
      }
      return 0;
    });

    console.log(
      sorted
      .filter(x=>x.name!=="Others")//remove "Others"
      .concat(sorted.find(x=>x.name==="Others"))//add "Others" at the end
    );

答案 1 :(得分:0)

您可以创建一个接受忽略列表的函数,并且可以返回一个排序函数,通过将它们放在列表的上方或下方来忽略某些名称。

您可以摆弄排序方向以获得所需的结果。

var BUSINESS_CATEGORIES = [
  { id: 1,  name: 'Food & Beverage' },
  { id: 2,  name: 'Transportation & Travel' },
  { id: 3,  name: 'Fashion' },
  { id: 4,  name: 'Leisure' },
  { id: 5,  name: 'Digital Content' },
  { id: 6,  name: 'Health and Beauty' },
  { id: 7,  name: 'Online Shopping' },
  { id: 8,  name: 'Telecommunications' },
  { id: 9,  name: 'Utilities' },
  { id: 10, name: 'Others' },
  { id: 11, name: 'General' },
  { id: 12, name: 'Insurance' },
  { id: 13, name: 'Services' },
  { id: 14, name: 'Appliances' },
  { id: 15, name: 'Entertainment' },
  { id: 16, name: 'Household Goods & Groceries' },
];

let categories = this.BUSINESS_CATEGORIES.sort(customSort([ 'Others' ], 1));

console.log(JSON.stringify(categories, null, 2));

function customSort(ignoreList, direction) {
  ignoreList = ignoreList || [];
  direction  = direction  || 1;
  return function(a, b) {
    var aPos = ignoreList.indexOf(a.name);
    var bPos = ignoreList.indexOf(b.name);

    if (bPos > -1) return -1 * direction;
    if (aPos > -1) return 1 * direction;

    return a.name.localeCompare(b.name) * direction;
  }
}
.as-console-wrapper { top: 0; max-height: 100% !important; }