简体   繁体   English

按内容对 object 属性进行排序

[英]sort object properties by his content

I need some help with this algorithm, i would like to get the sorted highest prices of Venta (sell), i can sort each pair content, but i can't realize how to sort the properties order too (by the highest price of first inner).我需要这个算法的一些帮助,我想得到 Venta(卖出)的排序最高价格,我可以对每对内容进行排序,但我也无法实现如何对属性顺序进行排序(按第一的最高价格内)。 Below is the code i have for now, and my expected output.下面是我现在拥有的代码,以及我预期的 output。 Thanks in advance.提前致谢。

 const data = { busd_ars: { belo: 'Invalid pair', binanceP2P: { Compra: 280, Venta: 278.41 }, bitso: { Compra: 283.6615, Venta: 279.4225 }, buenbit: { Compra: 285.2, Venta: 275.5 }, cryptoMkt: { Compra: 281.4251, Venta: 267.8393 } }, dai_ars: { belo: { Compra: 282.0846, Venta: 269.784 }, binanceP2P: { Compra: 281.45, Venta: 278.65 }, bitso: { Compra: 287.5624, Venta: 276.7951 }, buenbit: { Compra: 285.8, Venta: 274.8 }, cryptoMkt: { Compra: 279.6464, Venta: 267.7461 } }, usdc_ars: { belo: { Compra: 282.0846, Venta: 269.784 }, binanceP2P: 'Invalid pair', bitso: { Compra: 283.6615, Venta: 279.4205 }, buenbit: { Compra: 285.2, Venta: 275.5 }, cryptoMkt: { Compra: 279.5047, Venta: 267.8935 } }, usdt_ars: { belo: { Compra: 282, Venta: 270 }, binanceP2P: { Compra: 280.49, Venta: 279.15 }, bitso: { Compra: 287.1397, Venta: 276.4963 }, buenbit: { Compra: 285.2, Venta: 275.6 }, cryptoMkt: { Compra: 279.4978, Venta: 268.0277 } } } const getHighPrices = (stablesFiat) => { const result = {} for (let pair in stablesFiat) { let data = {} Object.entries(stablesFiat[pair]).filter(x => x[1].= 'Invalid pair'),sort((a. b) => b[1].Venta - a[1].Venta),slice(0. 3).forEach(x => { data[x[0]] = x[1].Venta }) result[pair] = data } return result } console:log(getHighPrices(data)) // expected output const result = { busd_ars, {}: usdc_ars, {}: usdt_ars, {}: dai_ars: {} }

You cannot sort a JavaScript object in an order.您不能按订单排序 JavaScript object。

In this thread (Does JS guarantee object property order) , you can see examples and excerpts from the documentation which says so.在此线程中(JS 是否保证 object 属性顺序) ,您可以从说明的文档中查看示例和摘录。

If you want to order data , you should not have it be an object, but instead an array.如果你想订购data ,你不应该是一个 object,而是一个数组。

For example:例如:

const data = [
    {
        name: 'busd_ars',
        belo: 'Invalid pair',
        binanceP2P: { Compra: 280, Venta: 278.41 },
        bitso: { Compra: 283.6615, Venta: 279.4225 },
        buenbit: { Compra: 285.2, Venta: 275.5 },
        cryptoMkt: { Compra: 281.4251, Venta: 267.8393 }
    },
    {
        name: 'dai_ars',
        belo: { Compra: 282.0846, Venta: 269.784 },
        binanceP2P: { Compra: 281.45, Venta: 278.65 },
        bitso: { Compra: 287.5624, Venta: 276.7951 },
        buenbit: { Compra: 285.8, Venta: 274.8 },
        cryptoMkt: { Compra: 279.6464, Venta: 267.7461 }
    },
    {
        name: 'usdc_ars',
        belo: { Compra: 282.0846, Venta: 269.784 },
        binanceP2P: 'Invalid pair',
        bitso: { Compra: 283.6615, Venta: 279.4205 },
        buenbit: { Compra: 285.2, Venta: 275.5 },
        cryptoMkt: { Compra: 279.5047, Venta: 267.8935 }
    },
    {
        name: 'usdt_ars',
        belo: { Compra: 282, Venta: 270 },
        binanceP2P: { Compra: 280.49, Venta: 279.15 },
        bitso: { Compra: 287.1397, Venta: 276.4963 },
        buenbit: { Compra: 285.2, Venta: 275.6 },
        cryptoMkt: { Compra: 279.4978, Venta: 268.0277 }
    }
]

Then you can sort this array by a certain property like:然后,您可以按某个属性对该数组进行排序,例如:

data.sort((a, b) => a.belo.Venta > b.belo.Venta) //just an example

Even if you do this and convert it back to an Object, there is no guarantee that it will stay in order .即使您这样做并将其转换回 Object,也不能保证它会保持正常

You could use the following function ( sortByHighestChildVentaValue ) to sort the dataset.您可以使用以下 function ( sortByHighestChildVentaValue ) 对数据集进行排序。 It creates an array from the initial object with a projection where each entry have the pair and highest Venta value for the markets where that pair is sold.它从初始 object 中创建一个数组,其中每个条目都具有该对和出售该对的市场的最高 Venta 值的投影。 Then, the array is sorted in descending order by the highest value and finally transform to an object.然后,数组按最大值降序排列,最后转换为 object。

That last step is discourage due to, as pointed out by @ cSharp in the other answer, this structure does not guaranty the order.最后一步是不鼓励的,因为正如@ cSharp在另一个答案中所指出的那样,这种结构不能保证顺序。

 const data = { busd_ars: { belo: 'Invalid pair', binanceP2P: { Compra: 280, Venta: 278.41 }, bitso: { Compra: 283.6615, Venta: 279.4225 }, buenbit: { Compra: 285.2, Venta: 275.5 }, cryptoMkt: { Compra: 281.4251, Venta: 267.8393 } }, dai_ars: { belo: { Compra: 282.0846, Venta: 269.784 }, binanceP2P: { Compra: 281.45, Venta: 278.65 }, bitso: { Compra: 287.5624, Venta: 276.7951 }, buenbit: { Compra: 285.8, Venta: 274.8 }, cryptoMkt: { Compra: 279.6464, Venta: 267.7461 } }, usdc_ars: { belo: { Compra: 282.0846, Venta: 269.784 }, binanceP2P: 'Invalid pair', bitso: { Compra: 283.6615, Venta: 279.4205 }, buenbit: { Compra: 285.2, Venta: 275.5 }, cryptoMkt: { Compra: 279.5047, Venta: 267.8935 } }, usdt_ars: { belo: { Compra: 282, Venta: 270 }, binanceP2P: { Compra: 280.49, Venta: 279.15 }, bitso: { Compra: 287.1397, Venta: 276.4963 }, buenbit: { Compra: 285.2, Venta: 275.6 }, cryptoMkt: { Compra: 279.4978, Venta: 268.0277 } } } const sortByHighestChildVentaValue = pairs => Object.keys(pairs) // Convert this to array for a better manipulation.map(p => { // Get the market where the pair have the max value for Venta const highestPerMarket = Object.keys(pairs[p]).map(market => ({ market: market, venta: pairs[p][market].Venta })).filter(market =>..market,venta).sort((ab) => b,venta - a.venta)[0] // return as an object with the pair name. eg: usdt_ars and the // max Venta value found, return { pair: p. highest. highestPerMarket,venta } }) // Sort by highest Venta value amongst the different pairs.sort((ab) => b.highest - a.highest) // This is to return an object instead of an array. Discourage to use // it as an object but required by question, .reduce((acc, curr) => { acc[curr.pair] = {} return acc }: {}) console,log(sortByHighestChildVentaValue(data)) // expected output const result = { busd_ars: {}, usdc_ars: {}, usdt_ars: {}, dai_ars: {} }

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

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