gpt4 book ai didi

javascript - 如果值不满足给定条件,如何将其他数组元素包含到新数组中

转载 作者:行者123 更新时间:2023-12-01 02:51:45 27 4
gpt4 key购买 nike

我这里有 1 个数组。如何在 JavaScript 中执行此逻辑

费率列表示例:

  var rates : [ 
{id:1,value:7},
{id:2,value:7},
{id:3,value:7},
{id:4,value:6},
{id:5,value:6},
{id:6,value:5},
{id:7,value:4},
]

条件是获得前5名的评分。最高为 7 .. 如果 7 小于 5 则包括比率 6 。如果所有费率小于 5。请包括费率 5。

注意:如果速率 7 小于 5 。所有速率可以大于5。参见其他场景

预期输出

{id:1,value:7},  
{id:2,value:7},
{id:3,value:7},
{id:4,value:6},
{id:5,value:6},

其他场景

 var rates : [ 
{id:1,value:7},
{id:2,value:7},
{id:3,value:7},
{id:4,value:7},
{id:5,value:5},
{id:5,value:5},
{id:5,value:5},
{id:6,value:4},
{id:7,value:4},
]

输出

{id:1,value:7},  
{id:2,value:7},
{id:3,value:7},
{id:4,value:7},
{id:5,value:5},
{id:5,value:5},
{id:5,value:5},

非常感谢您的帮助

最佳答案

这是您问题的伪代码答案:

/*Sort the rates highest to lowest by value*/

/*Create a new array to store your top rates*/

/*iterate over your rates */

/* If there are less than 5 topRates go ahead and add the rate
to top rates.
Remember rates is now sorted highest to lowest value
*/

/* else if we have the top 5 already, check to see if
the current rate is equal to the last top rate
*/

/* otherwise our topRates has more than 5 and the next rate is
not the same value of our last top rate break out of the loop
*/

关于javascript - 如果值不满足给定条件,如何将其他数组元素包含到新数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46921937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com