筛选和排序JavaScript数组

时间:2016-09-03 03:08:05

标签: javascript arrays sorting

我有一个这样的数组:

[{
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 6.4
    }
}, {
    "number": "3",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}, {
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}]

我正在尝试使用以下条件创建一个新数组:

  1. 获得每个号码(array.rating.average
  2. 的最高评分(array.number

    输出应为:

    [{
        "number": "4",
        "fileName": "fileXX",
        "rating": {
            "average": 6.4
        }
    }, {
        "number": "3",
        "fileName": "fileXX",
        "rating": {
            "average": 5.4
        }
    }
    }]
    

    我刚刚按最高评分排序:

    array.sort(function(a , b) {
        return a.rating.average - b.rating.average;
    });
    array.reverse();
    

    但是,现在,我只需要每个重复array.number一个对象,保留一个具有最高array.rating.average的对象。

4 个答案:

答案 0 :(得分:3)

array.sort((a, b) => {
  if(a.number === b.number) {
    // If two elements have same number, then the one who has larger rating.average wins
    return b.rating.average - a.rating.average;
  } else {
    // If two elements have different number, then the one who has larger number wins
    return b.number - a.number;
  }
});


array = array.filter((element, index) => {
  return index === 0 || element.number !== array[index-1].number;
});

对于您的测试用例,

[{
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 6.4
  }
}, {
  "number": "3",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}, {
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}]

排序后,输出为

[{
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 6.4
  }
}, {
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}, {
  "number": "3",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}]

过滤后,最终结果:

[{
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 6.4
  }
}, {
  "number": "3",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}]

答案 1 :(得分:1)

您可以使用lodash方法对要按文件名,数字等进行排序的任何内容进行排序。

node-pre-gyp ERR! node -v v12.1.0
node-pre-gyp ERR! node-pre-gyp -v v0.10.3
node-pre-gyp ERR! not ok

答案 2 :(得分:0)

arr = [{
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 6.4
    }
}, {
    "number": "3",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}, {
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}]

arr = arr.sort(function(a,b){return b.number-a.number || b.rating.average-a.rating.average}).filter(function(a,b,c){return !b || c[b-1].number != a.number});
console.log(arr);

我相信这会通过排序和过滤来解决您的问题。

编辑:现在应该支持旧浏览器了。

答案 3 :(得分:0)

首先,您创建一个字典,以保持每个数字的最高评级:



var data = [{
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 6.4
    }
}, {
    "number": "3",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}, {
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}];


var filterMap = {};
data.forEach(function (item) {
  if (!filterMap[item.number] || filterMap[item.number].rating.average < item.rating.average) {
    filterMap[item.number] = item;
  }
})

var result = [];

for (var number in filterMap) {
  result.push(filterMap[number]);
}


result.sort(function(a , b) {
   return b.rating.average - a.rating.average;
});

console.log(result);
&#13;
&#13;
&#13;