简体   繁体   English

Javascript 按数组的嵌套 object 排序

[英]Javascript sort by nested object of array

    {
        "a": [
            {
            "value1": 34,
            "value2": 44
            },
            {
            "value1": 33,
            "value2": 10
            },
            {
            "value1": 52,
            "value2": 53
            }
        ],
        "b": [
            {
            "value1": 12,
            "value2": 43
            },
            {
            "value1": 23,
            "value2": 78
            },
            {
            "value1": 98,
            "value2": 36
            }
        ],
        "c": [
            {
            "value1": 56,
            "value2": 90
            },
            {
            "value1": 76,
            "value2": 50
            },
            {
            "value1": 20,
            "value2": 30
            }
        ]
    }

Here is my data.这是我的数据。 I am trying to sort this data by "value2" of last object of array.我正在尝试按数组的最后一个 object 的“value2”对这些数据进行排序。

So.所以。 Here "value2" of "c" is 30 which is small so that object should come first.这里“c”的“value2”是 30,它很小,所以 object 应该排在第一位。

The expected result is:预期结果是:

["c", "b", "a"]

So "value2" order is is 30 then 36 then 53所以“value2”的顺序是 30 然后 36 然后 53

I can't think of any way to do this so asking here without attempt.我想不出任何办法来做到这一点,所以没有尝试就在这里问。

Please take a look how we can achieve this.请看看我们如何实现这一目标。

You can do it in this way你可以这样做

 const data = { a: [ { value1: 34, value2: 44, }, { value1: 33, value2: 10, }, { value1: 52, value2: 53, }, ], b: [ { value1: 12, value2: 43, }, { value1: 23, value2: 78, }, { value1: 98, value2: 36, }, ], c: [ { value1: 56, value2: 90, }, { value1: 76, value2: 50, }, { value1: 20, value2: 30, }, ], }; const sortData = (data) => { const obj = {}; for (const i in data) { obj[i] = data[i][data[i].length - 1]["value2"]; } return Object.entries(obj).sort(([, a], [, b]) => a - b).reduce((r, [k]) => ({...r, [k]: data[k] }), {}); }; const res = sortData(data); console.log(res);

By nature, Objects don't inherently have a guaranteed sort order, however, we can try to work around that which should work in most cases.从本质上讲,对象本身并没有保证的排序顺序,但是,我们可以尝试解决在大多数情况下应该可行的问题。

Objects don't have a .sort() method, but we can use Object.entries() to temporarily convert the object to an array, apply our desired sorting logic, and then reconvert the final product to an object using Object.fromEntries() . Objects don't have a .sort() method, but we can use Object.entries() to temporarily convert the object to an array, apply our desired sorting logic, and then reconvert the final product to an object using Object.fromEntries() .

This should do what you're looking for:这应该可以满足您的需求:

const sortObjectByValue2 = obj => Object.fromEntries(Object.entries(obj).sort((a,b) => a[1].slice().reverse()[0].value2 - b[1].slice().reverse()[0].value2));

Here it is in action:这是在行动:

 const sortObjectByValue2 = obj => Object.fromEntries(Object.entries(obj).sort((a,b) => a[1].slice().reverse()[0].value2 - b[1].slice().reverse()[0].value2)); const values = { a: [{ value1: 34, value2: 44 }, { value1: 33, value2: 10 }, { value1: 52, value2: 53 }], b: [{ value1: 12, value2: 43 }, { value1: 23, value2: 78 }, { value1: 98, value2: 36 }], c: [{ value1: 56, value2: 90 }, { value1: 76, value2: 50 }, { value1: 20, value2: 30 }] } console.log(sortObjectByValue2(values));

More information on Object.entries() and Object.fromEntries() can be found at these links:有关Object.entries()Object.fromEntries()的更多信息,请访问以下链接:

I think you can store the keys of the object to an array, and then sorted the array by the object vaule.我认为您可以将 object 的键存储到一个数组中,然后按 object 值对数组进行排序。 Could use the code like the following:可以使用如下代码:

const unordered  = {
    "a": [
        {
        "value1": 34,
        "value2": 44
        },
        {
        "value1": 33,
        "value2": 10
        },
        {
        "value1": 52,
        "value2": 53
        }
    ],
    "b": [
        {
        "value1": 12,
        "value2": 43
        },
        {
        "value1": 23,
        "value2": 78
        },
        {
        "value1": 98,
        "value2": 36
        }
    ],
    "c": [
        {
        "value1": 56,
        "value2": 90
        },
        {
        "value1": 76,
        "value2": 50
        },
        {
        "value1": 20,
        "value2": 30
        }
    ]
};

let ordered = Object.keys(unordered).sort((k1,k2)=>{
        let arr1 = unordered[k1];
        let arr2 = unordered[k2];
        if(arr1[arr1.length-1]["value2"]>arr2[arr2.length-1]["value2"]){
            return 1;
        }else{
            return -1;
        }
    });

console.log(ordered);

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

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