简体   繁体   English

在对象数组中对数组进行排序

[英]Sort an array inside an array of objects

if I have a big collection of array, what is the fastest way to sort and return the first 5 items?如果我有大量数组,排序和返回前 5 个项目的最快方法是什么?

Array of Object:对象数组:

[
  {
    "name" : "placeA",
    "menus" : [ 
        {
            "sold" : 5,
            "name" : "menuA"
        }, 
        {
            "sold" : 20,
            "_id" : "menuB"
        }, 
        {
            "sold" : 1000,
            "_id" : "menuC"
        }
    ]
  },
  {
    "name" : "placeB",
    "menus" : [ 
        {
            "sold" : 300,
            "name" : "menuD"
        }, 
        {
            "sold" : 400,
            "_id" : "menuE"
        }, 
        {
            "sold" : 50,
            "_id" : "menuF"
        }
    ]
  },
  {
    "name" : "placeC",
    "menus" : [ 
        {
            "sold" : 1500,
            "name" : "menuG"
        }, 
        {
            "sold" : 450,
            "_id" : "menuH"
        }, 
        {
            "sold" : 75,
            "_id" : "menuI"
        }
    ]
  }
]

Expected Output:预期输出:

[
    {
      "sold" : 1500,
      "name" : "menuG"
    },
    {
      "sold" : 1000,
      "_id" : "menuC"
    },
    {
      "sold" : 450,
      "_id" : "menuH"
    },
    {
      "sold" : 400,
      "_id" : "menuE"
    },
    {
      "sold" : 300,
      "name" : "menuD"
    } 
]

The only possible way that I'm able to think of is to create an array that is filled all the menus and then sort the menus and then slice the first 5 items.我能想到的唯一可能的方法是创建一个填充所有菜单的数组,然后对菜单进行排序,然后对前 5 个项目进行切片。 I don't think this way is good enough for sorting a huge collection of array in real use case.我认为这种方式不足以在实际用例中对大量数组集合进行排序。

You can use .map() , .reduce() , .concat() , .sort() , and .slice() .您可以使用.map() .reduce() .concat() .sort().slice()

 var arr = [{ "name": "placeA", "menus": [{ "sold": 5, "name": "menuA" }, { "sold": 20, "_id": "menuB" }, { "sold": 1000, "_id": "menuC" } ] }, { "name": "placeB", "menus": [{ "sold": 300, "name": "menuD" }, { "sold": 400, "_id": "menuE" }, { "sold": 50, "_id": "menuF" } ] }, { "name": "placeC", "menus": [{ "sold": 1500, "name": "menuG" }, { "sold": 450, "_id": "menuH" }, { "sold": 75, "_id": "menuI" } ] } ] var newarr = arr.map(v => v.menus).reduce((a, c) => a.concat(c)); console.log(newarr.sort((a,b) => b.sold - a.sold).slice(0,5));

I'm not sure if this is the fastest way, though.不过,我不确定这是否是最快的方式。

In addition to what Yousername said, you can also you flat :除了 Yousername 所说的,你也可以你flat

array
.map((a) => a.menus) // select menus
.flat() // all sub-array elements concatenated
.sort((a, b) => b.sold - a.sold) // sort by key sold
.slice(0,5) // select the first 5

You can use .reduce() , and clean it up with spread operator to flatten the Arrays as you go, and destructing to get straight to the 'menus' Array您可以使用.reduce() ,并使用扩展运算符清理它以在您进行时展平数组,并销毁以直接进入“菜单”数组

 const data = [{name:"placeA",menus:[{sold:5,name:"menuA"},{sold:20,_id:"menuB"},{sold:1e3,_id:"menuC"}]},{name:"placeB",menus:[{sold:300,name:"menuD"},{sold:400,_id:"menuE"},{sold:50,_id:"menuF"}]},{name:"placeC",menus:[{sold:1500,name:"menuG"},{sold:450,_id:"menuH"},{sold:75,_id:"menuI"}]}]; let newArray = data .reduce((acc, {menus}) => [...acc, ...menus], []) .sort((a, b) => b.sold - a.sold); console.log(newArray);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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

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