es6:使用条件语句对对象数组进行排序

时间:2016-10-27 16:52:14

标签: javascript arrays sorting ecmascript-6

我在我的对象数组上使用排序数组原型函数 - 按'min'键排序

function setStatesOrder(stateOrderArr){

   return stateOrderArr.sort((x,y) => x.min - y.min);

}

但是现在我想保留我的对象数组中的第一个对象。它有“国家”的关键和“美国”的价值所以我试图通过它排除这个对象,但它不起作用:

  function setStatesOrder(stateOrderArr){

     return stateOrderArr.sort((x,y) => {if(x.state !=="U.S."){x.min - y.min}});

  }


stateOrderArr  = [
{state: "U.S.", max: 1, min: -14, stateID: "US", final: 0.3},
{state: "Ohio", max: 5, min: -2, stateID: "OH", final: 5},
{state: "Georgia", max: 4, min: -5, stateID: "GA", final: -5},
{state: "Arizona", max: -1.5, min: -2, stateID: "AZ", final: -2}....]

5 个答案:

答案 0 :(得分:0)

我相信你正在寻找

function setStatesOrder(stateOrderArr){
    return stateOrderArr.sort((x,y) => (y.state=="U.S.")-(x.state=="U.S.") || x.min - y.min);
}

如果它们应该以某种方式订购,你不能只是“不比较”两个项目。要么不将它们放在首先排序的数组中,要么通过始终处理.state == "U.S."小于其他所有项目的项目来查找特殊处理它们的比较。

答案 1 :(得分:0)

stateOrderArr.sort(
  (x,y) => x.state === "U.S". ? -1 : y.state === "U.S." ? +1 ? x.min - y.min);

换句话说,说美国总是比什么都少。

然而,首先排序可能更具语义性,然后将美国排在前面。这是将事情移到前线的一个小例程:

function moveToFront(array, condition) {
  return array.splice(array.findIndex(condition), 1).concat(array);
}

这利用了Array.splice返回已删除项目的事实。

然后

moveToFront(
  array.sort((a, b) => a.min - b.min),
  a => a.state === "U.S.");

答案 2 :(得分:0)

如果你不介意返回一个新的数组而不是修改旧的数组,那么它就会更清洁(在我看来)不要弄乱.sort函数并且只是明确地排除第一个项目从排序:

function setStatesOrder(stateOrderArr) {
  const [firstState, ...restOfStates] = stateOrderArr;
  restOfStates.sort((x,y) => x.min - y.min);
  return [firstState, ...restOfStates];

  //The previous two lines can also be combined, as
  //return [firstState, ...restOfStates.sort((x,y) => x.min - y.min)];
}

const stateOrderArr  = [
{state: "U.S.", max: 1, min: -14, stateID: "US", final: 0.3},
{state: "Ohio", max: 5, min: -2, stateID: "OH", final: 5},
{state: "Georgia", max: 4, min: -5, stateID: "GA", final: -5},
{state: "Arizona", max: -1.5, min: -2, stateID: "AZ", final: -2}];

console.log(setStatesOrder(stateOrderArr))

答案 3 :(得分:-1)

只需在排序前过滤您的数组:

function setStatesOrder(stateOrderArr){
   return stateOrderArr
     .filter(e => e.state !== "U.S.")
     .sort((x,y) => x.min - y.min);
}


stateOrderArr  = [
  {state: "U.S.", max: 1, min: -14, stateID: "US", final: 0.3},
  {state: "Ohio", max: 5, min: -2, stateID: "OH", final: 5},
  {state: "Georgia", max: 4, min: -5, stateID: "GA", final: -5},
  {state: "Arizona", max: -1.5, min: -2, stateID: "AZ", final: -2}]

const ordered = setStatesOrder(stateOrderArr);
ordered.forEach(e => console.log(e.state));

Georgia
Ohio
Arizona

您可以在此处运行:https://runkit.com/arthur/5812319a2e7a3c0014799825

答案 4 :(得分:-1)

排序谓词总是需要返回一个值。我认为这是一个使用三元运算符的好地方。表格是“[条件]?[如果是]:[如果错误]”

function setStatesOrder(stateOrderArr){

  return stateOrderArr.sort((x,y) => x.state === "U.S." ? -1 : x.min - y.min);

}

var stateOrderArr  = [
{state: "U.S.", max: 1, min: -14, stateID: "US", final: 0.3},
{state: "Ohio", max: 5, min: -2, stateID: "OH", final: 5},
{state: "Georgia", max: 4, min: -5, stateID: "GA", final: -5},
{state: "Arizona", max: -1.5, min: -2, stateID: "AZ", final: -2}]

document.write(setStatesOrder(stateOrderArr).map((x) => x.state));

另一种有效的方法是删除您不想排序的元素,并在完成后将它们放回去。对于一个元素,您可以使用Array.prototype.shift在排序之前取消第一个元素,然后使用Array.prototype.unshift将其重新打开。我不认为你的案件存在可衡量的差异。

function setStatesOrder(stateOrderArr){
  let first = stateOrderArr.shift();
  stateOrderArr = stateOrderArr.sort((x,y) => x.min - y.min);
  stateOrderArr.unshift(first);
  return stateOrderArr;

}

var stateOrderArr  = [
{state: "U.S.", max: 1, min: -14, stateID: "US", final: 0.3},
{state: "Ohio", max: 5, min: -2, stateID: "OH", final: 5},
{state: "Georgia", max: 4, min: -5, stateID: "GA", final: -5},
{state: "Arizona", max: -1.5, min: -2, stateID: "AZ", final: -2}]

document.write(setStatesOrder(stateOrderArr).map((x) => x.state));