简体   繁体   English

用另一个数组对较大的javascript数组进行排序,然后将值移到该数组的顶部

[英]sort larger javascript array by another array and move values to the top of the array

I apologize if this has been asked before but I was looking to sort a javascript array based on the criteria of another array. 很抱歉,如果以前有人问过这个问题,但我想根据另一个数组的条件对javascript数组进行排序。 I know that all of the values contained in the criteria array will be in the larger array somewhere but I need the ones from the criteria array to be moved to the top of the list. 我知道criteria数组中包含的所有值都将在某个地方的较大数组中,但是我需要将criteria数组中的值移到列表顶部。

For example I have a larger array that looks something like this : 例如,我有一个更大的数组,看起来像这样:

array = [
  {_id: "123", username: "username1",platform: "xbox"},
  {_id: "124", username: "username2",platform: "xbox"},
  {_id: "125", username: "username3",platform: "ps4"},
  {_id: "126", username: "username4",platform: "pc"},
  {_id: "127", username: "username5",platform: "ps4"},
  {_id: "128", username: "username6",platform: "pc"}
    ];

I then need to sort this larger array based on the criteria of the smaller array and then move those values to the top of the larger one 然后,我需要根据较小数组的条件对该较大数组进行排序,然后将这些值移至较大数组的顶部

So in this case the criteria array would be 因此,在这种情况下,条件数组为

critarray = [
 {_id: "124", username: "username2",platform: "xbox"},
 {_id: "127", username: "username5",platform: "ps4"},
 {_id: "128", username: "username6",platform: "pc"},
];

The the larger array would be sorted as follows: 较大的数组将排序如下:

array = [
  {_id: "124", username: "username2",platform: "xbox"},
  {_id: "127", username: "username5",platform: "ps4"},
  {_id: "128", username: "username6",platform: "pc"},
  {_id: "123", username: "username1",platform: "xbox"},
  {_id: "125", username: "username3",platform: "ps4"},
  {_id: "126", username: "username4",platform: "pc"},
    ];

Also it doesn't really matter the order as long as all the objects in the criteria array are moved to the top. 同样,只要条件数组中的所有对象都移到顶部,顺序就没有关系。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks 谢谢

Filter and concat 过滤并合并

Since you don't care about the order other than these two groups and you know the larger array has all the elements from the smaller, rather than sorting, you could just filter all critarray items out of array than concatenate critarray and array : 因为除了这两个组之外您都不关心顺序,并且您知道较大的数组具有较小的所有元素,而不是进行排序,所以可以将所有critarray项从array过滤掉,而不是将critarrayarray串联:

 let array = [ {_id: "123", username: "username1",platform: "xbox"}, {_id: "124", username: "username2",platform: "xbox"}, {_id: "125", username: "username3",platform: "ps4"}, {_id: "126", username: "username4",platform: "pc"}, {_id: "127", username: "username5",platform: "ps4"}, {_id: "128", username: "username6",platform: "pc"} ]; let critarray = [ {_id: "124", username: "username2",platform: "xbox"}, {_id: "127", username: "username5",platform: "ps4"}, {_id: "128", username: "username6",platform: "pc"}, ]; // all items not in crit array let arrayFiltered = array.filter(item => !critarray.find(i => i._id === item._id) ) // concat critarray first, then rest let newArr = critarray.concat(arrayFiltered) console.log(newArr) 

This assumes _id is a unique identifier. 假定_id是唯一标识符。

or Sort 或排序

Alternatively, you could also make a Set of ids from citarray and use inclusion in that set as the basis for sorting if you want to sort the array in place: 另外,如果您想对数组进行排序,则还可以从citarraySet ID,并使用其中的包含作为排序的基础:

 let array = [ {_id: "123", username: "username1",platform: "xbox"}, {_id: "124", username: "username2",platform: "xbox"}, {_id: "125", username: "username3",platform: "ps4"}, {_id: "126", username: "username4",platform: "pc"}, {_id: "127", username: "username5",platform: "ps4"}, {_id: "128", username: "username6",platform: "pc"}, ]; let critarray = [ {_id: "124", username: "username2",platform: "xbox"}, {_id: "127", username: "username5",platform: "ps4"}, {_id: "128", username: "username6",platform: "pc"}, ]; let critIds = new Set critarray.forEach(item => critIds.add(item._id)) array.sort((a, b) => critIds.has(b._id) - critIds.has(a._id)) console.log(array) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM