根据 属性 和 return 排序的初始对象从对象排序数组

Sort array from object based on a property and return the sorted initial object

我有以下对象

{
  "object": "list",
  "url": "/v1/prices",
  "has_more": false,
  "data": [
    {
      "id": "price_1KHlU72eZvKYlo2CblI51Z8e",
      "object": "price",
      "active": true,
      "billing_scheme": "per_unit",
      "created": 1642150211,
      "currency": "usd",
      "livemode": false,
      "lookup_key": null,
      "metadata": {},
      "nickname": null,
      "product": "prod_Kxgr3hZDfHnqu1",
      "recurring": {
        "aggregate_usage": null,
        "interval": "month",
        "interval_count": 1,
        "usage_type": "licensed"
      },
      "tax_behavior": "unspecified",
      "tiers_mode": null,
      "transform_quantity": null,
      "type": "recurring",
      "unit_amount": 2,
      "unit_amount_decimal": "2"
    },
    {...},
    {...}
  ]
}

如何根据 unit_amount 属性 对 object.data 进行排序并保留对象的初始值 (url, has_more, ...)?

我试过使用 Ramda,但它只为我提供了一个包含排序数据的新对象。

所以我需要 object.data 从要排序的初始对象到 return 排序后的整个对象。

只是简单的排序,例如

const obj = {
  "object": "list",
  "url": "/v1/prices",
  "has_more": false,
  "data": [
    {"unit_amount": 2,},
    {"unit_amount": 1,},
    {"unit_amount": 3,},
    {"unit_amount": 9,},
    {"unit_amount": 5,},
  ]
};

const newObj = { ...obj, data: obj.data.sort((o1, o2) => o1.unit_amount - o2.unit_amount) }; 

console.log(newObj)
.as-console-wrapper {max-height: 100% !important; top: 0}

Javascript 中的对象没有固有的属性顺序,而数组有,所以我建议您提取数据数组,对其进行排序,然后替换对象中未排序的版本。像这样的东西(为便于阅读而简化的对象):

o = {
    "object": "list",
        "url": "/v1/prices",
            "has_more": false,
                "data": [
                    {
                        "id": "price_1KH",
                        "unit_amount": 12,
                    },
                    {
                        "id": "price_7QW",
                        "unit_amount": 3,
                    },
                    {
                        "id": "price_4DD",
                        "unit_amount": 7,
                    },
                ]
}
let data = o.data;
data.sort((x, y) => x.unit_amount - y.unit_amount);
o.data = data;
console.dir(o);
VM694:23 
Object
data: Array(3)
0: {id: 'price_7QW', unit_amount: 3}
1: {id: 'price_4DD', unit_amount: 7}
2: {id: 'price_1KH', unit_amount: 12}
length: 3
[[Prototype]]: Array(0)
has_more: false
object: "list"
url: "/v1/prices"
[[Prototype]]: Object

Ramda 不会让您轻松修改数据,但如果您想使用数组的排序副本创建输入副本,使用镜头函数 lensPropover:

const sortData = over (lensProp ('data'), sortBy (prop ('unit_amount'))) 

const obj = {object: "list", url: "/v1/prices", has_more: false, data: [{unit_amount: 2,}, {unit_amount: 1,}, {unit_amount: 3,}, {unit_amount: 9,}, {unit_amount: 5,} ]};

console .log (sortData (obj))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.2/ramda.min.js"></script>
<script>const {over, lensProp, sortBy, prop} = R </script>

这里我们从 Alexandr Belan 那里窃取了简单的输入格式。该答案已经有一些简单的代码。我认为这更简单——而且它肯定更具声明性——但除非我已经在我的应用程序中使用 Ramda,否则我不会为此烦恼。