【问题标题】:custom order rules for array of object对象数组的自定义顺序规则
【发布时间】:2020-12-09 03:54:16
【问题描述】:

如何按对象数组的规则进行排序。据我所知,与对象不同,通过使用数组,项目的顺序是固定的

[{name: 'james', name: 'alice', name: 'sam'}]

所以如果我映射打印上面的列表,顺序将是 james,alice sam。如果我想按照一套规则订购上述商品怎么办?

规则:

  1. 詹姆斯、爱丽丝、山姆(默认)
  2. 爱丽丝、山姆、詹姆斯
  3. 山姆、詹姆斯、爱丽丝

有没有像 lodash 这样的助手可以做到这一点?基本上我需要一个功能

function sortByRules(rules) { //do something with the array }

【问题讨论】:

  • 规则是什么意思。你需要传递哪些规则?
  • 它可以是 3 个数组,例如 ['james', 'alice', 'sam'] 或 ['alice', 'sam', 'james'] 或 ['sam', 'james' , '爱丽丝']

标签: javascript lodash


【解决方案1】:
var data = [{
    name: 'james'
  },
  {
    name: 'alice'
  }, {
    name: 'sam'
  }
];

console.log(data.sort((a,b) => {
    if(a.name === 'alice'){
  return -1;
  } else {
  return 1;
  }
}));

基本上,当您比较项目时,您返回 1 表示向下移动 a,返回 -1 表示向上移动 b,或者返回 0 表示不移动任何东西。

【讨论】:

  • 我需要传入一组规则,但是 === 'alice' 会解决问题,我好像没看懂问题
  • 你想要编程的规则是什么样的?
  • 它可以是 3 个数组,例如 ['james', 'alice', 'sam'] 或 ['alice', 'sam', 'james'] 或 ['sam', 'james' , '爱丽丝']
  • 好的,你想做什么?您是要上移某些名称还是下移某些名称?
【解决方案2】:

首先我认为你的数组格式不正确,应该是

[{name: 'james'}, {name: 'alice'}, {name: 'sam'}]

而不是

[{name: 'james', name: 'alice', name: 'sam'}]

现在我们需要理解为什么你所说的规则是什么,你想要传递一个数组像['james', 'alice']这样的人并首先获取这些值,然后你可以这样做:

const people = [
  { name: 'james' },
  { name: 'alice' },
  { name: 'sam' },
  { name: 'will' },
];

const sortByRules = (arr, rules = []) => {
  const inRules = arr.filter((person) => rules.includes(person.name));

  inRules.sort((a, b) => rules.indexOf(a.name) - rules.indexOf(b.name));

  return [...inRules, ...arr.filter((person) => !rules.includes(person.name))];
};

console.log(sortByRules(people, ['alice', 'sam', 'james']));

【讨论】:

    【解决方案3】:

    您可以尝试将sort 用作data.sort((a, b) => rule.indexOf(a.name) - rule.indexOf(b.name));。这里rule 是您想要对数组进行排序的任何规则。

    如果您在data 中有更多不包含在rule 中的项目,那么它将出现在结果的开头并带有sortByRules 函数。

    如果您希望它出现在最后,请使用sortByRules2

    在下面试试。

    var data = [
      { name: 'james' },
      { name: 'alice' },
      { name: 'sam' },
      { name: 'bob' }
    ];
    
    var rules = [
      ['james', 'alice', 'sam'],
      ['alice', 'sam', 'james'], 
      ['sam', 'james', 'alice']
    ];
    
    // names which are not included in rule will appear first.
    function sortByRules(rule) { 
      data.sort((a, b) => rule.indexOf(a.name) - rule.indexOf(b.name));
      console.log(data);
    }
    
    // names which are not included in rule will appear last.
    function sortByRules2(rule) {
      data.sort((a, b) => {
        let aIndex = rule.indexOf(a.name);
        let bIndex = rule.indexOf(b.name);
        aIndex = aIndex === -1 ? data.length : aIndex;
        bIndex = bIndex === -1 ? data.length : bIndex;
        return aIndex - bIndex
      });
      console.log(data);
    }
    
    sortByRules(rules[1]);
    sortByRules2(rules[1]);

    【讨论】:

    • 如果我的数组中有更多项目,这会起作用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2012-06-24
    相关资源
    最近更新 更多