简体   繁体   English

对对象数组进行排序?

[英]Sorting an array of objects?

I have the following data structure and I'm stuck on a way to sorting all variants from all items in ascending order based on price.我有以下数据结构,并且我坚持一种基于价格以升序对所有项目中的所有变体进行排序的方法。

Ideally I need a way to find the lowest price of any item variant while still having access to the variant's parent.理想情况下,我需要一种方法来找到任何项目变体的最低价格,同时仍然可以访问变体的父项。

var items = [
{
    "id" : 1 ,
    "name" : "name1",
    "variations" : [
        {
            "subId" : 1000011,
            "subName" : "name1a",
            "price" : 19.99
        },
        {
            "subId" : 1000012,
            "subName" : "name1b",
            "price" : 53.21
        },
        {
            "subId" : 1000013,
            "subName" : "name1c",
            "price" : 9.49

        }
    ]

},
{
    "id" : 2, 
    "name" : "name2",
    "variations" : [
        {
            "subId" : 1000021,
            "subName" : "name2a",
            "price" : 99.29
        },
        {
            "subId" : 1000022,
            "subName" : "name2b",
            "price" : 104.99
        },
        {
            "subId" : 1000023,
            "subName" : "name2c",
            "price" : 38.00

        }
    ]

}

];

You can use the following code in order to do it:您可以使用以下代码来执行此操作:

items.forEach(function(item){
  var variations = item.variations.sort(function(a, b){
    return b.price - a.price;
  });
  console.log(variations);
});

This function is going to be called for every item inside of the items array.将为items数组中的每个项目调用此函数。 If you want to order only the first, or second or n item in the items array you need to use something like:如果您只想订购 items 数组中的第一个、第二个或n项,则需要使用以下内容:

var variations = items[0].variations.sort(function(a, b){
  return b.price - a.price;
});
console.log(variations);

Where items[0] represents the item you want to order.其中items[0]表示您要订购的商品。

As you can see:如你看到的:

function(a, b){
  return b.price - a.price;
}

Is the function that orders the final result of the every array item from the items array.是从items数组中对每个数组item的最终结果进行排序的函数。 Using the sort function .使用sort功能 You can read more about how the compare function works to sort the items of an array.您可以阅读更多关于 compare 函数如何对数组的项目进行排序的信息。

the array elements are sorted according to the return value of the compare function数组元素根据比较函数的返回值排序

Check out this jsbin example :看看这个jsbin 示例

var sortedByPrice = items.map(function(item) {
  item.variations.sort(function(a, b) {
    return a.price - b.price;
  });

  return item;
});

console.log(sortedByprice);

Just map through them and sort based on the variation's price.只需映射它们并根据变体的价格进行排序。

You have some syntax errors / mistypes in your .json.您的 .json 中有一些语法错误/错误类型。 after fix you can use something like this:修复后你可以使用这样的东西:

var items = [
{
    "id" : 1 ,
    "name" : "name1",
    "variations" : [
        {
            "subId" : 1000011,
            "subName" : "name1a",
            "price" : 19.99
        },
        {
          "subId" : 1000012,
            "subName" : "name1b",
            "price" : 53.21
        },
        {
          "subId" : 1000013,
            "subName" : "name1c",
            "price" : 9.49

        }
    ]

},
{
    "id" : 2, 
    "name" : "name2",
    "variations" : [
        {
            "subId" : 1000021,
            "subName" : "name2a",
            "price" : 99.29
        },
        {
          "subId" : 1000022,
            "subName" : "name2b",
            "price" : 104.99
        },
        {
          "subId" : 1000023,
            "subName" : "name2c",
            "price" : 38.00

        }
    ]

}

];
items;

items.sort(function(a,b){
  var mina=a.variations.sort(function(x,y){
       return x.price - y.price;
     })[0].price; //find minimal price in 1st variations
  var minb=b.variations.sort(function(x,y){
       return x.price - y.price;
    })[0].price; // in 2nd variations
return mina - minb; //what compare returns 
});

Update 1更新 1

If you want more structural solution use this:如果您想要更多结构化解决方案,请使用以下命令:

function minProp(arr, prop){
  return arr.sort(function(a,b){
    return a[prop] - b[prop];
  })[0][prop];
}

items.sort(function(a,b){
  var mina = minProp(a.variations,'price');
  var minb = minProp(b.variations,'price');
return mina-minb;  
});

More structure may be invoked.可以调用更多的结构。

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

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