javascript - 在 Javascript 中,如何在满足某些条件的情况下查找数组中具有最高值的特定属性的对象

标签 javascript arrays sorting oop object

有一个名为 householdArray 的数组,其中填充有两个属性的对象:第一个是 isSelling,其值为 true >或。第二个是askingPrice,它的值是一个数字。还有另一个名为 buyer 的对象,它有一个带有数值的 budget 属性。

如何在 householdArray 中具有 isSelling === true 的所有对象中找到具有最高 askingPrice 的对象在买家预算之内吗?

最佳答案

您可以使用过滤器和最小值最大值,但我会利用 Array.prototype.reduce() 在 O(n) 时间内一次性完成这项工作。为了鼓励您使用箭头函数,我将首先向您展示箭头,然后向您展示传统的函数实现。

请注意,我们使用像 {isSelling: true, AskPrice: 0} 这样的对象作为归约操作的初始值。

var buyer = {budget:1000},
household = [{  isSelling: true,
              askingPrice: 500},
             {  isSelling: false,
              askingPrice: 500},
             {  isSelling: true,
              askingPrice: 1000},
             {  isSelling: true,
              askingPrice: 790},
             {  isSelling: false,
              askingPrice: 1000},
             {  isSelling: true,
              askingPrice: 1200},
             {  isSelling: false,
              askingPrice: 690},
             {  isSelling: true,
              askingPrice: 890},
             {  isSelling: true,
              askingPrice: 1500},
             {  isSelling: true,
              askingPrice: 790},
             {  isSelling: true,
              askingPrice: 900},
             {  isSelling: true,
              askingPrice: 990},
             {  isSelling: false,
              askingPrice: 990},
             {  isSelling: true,
              askingPrice: 670}
            ],
   result = household.reduce((p,c) => c.isSelling === true          &&
                                      c.askingPrice <= buyer.budget &&
                                      c.askingPrice > p.askingPrice ? c : p,{isSelling: true, askingPrice: 0});
console.log(result);

现在有了传统的函数实现

var buyer = {budget:1000},
household = [{  isSelling: true,
              askingPrice: 500},
             {  isSelling: false,
              askingPrice: 500},
             {  isSelling: true,
              askingPrice: 670},
             {  isSelling: true,
              askingPrice: 790},
             {  isSelling: false,
              askingPrice: 1000},
             {  isSelling: true,
              askingPrice: 1200},
             {  isSelling: false,
              askingPrice: 690},
             {  isSelling: true,
              askingPrice: 890},
             {  isSelling: true,
              askingPrice: 1500},
             {  isSelling: true,
              askingPrice: 790},
             {  isSelling: true,
              askingPrice: 900},
             {  isSelling: true,
              askingPrice: 990},
             {  isSelling: false,
              askingPrice: 990},
             {  isSelling: true,
              askingPrice: 1000}
            ],
   result = household.reduce(function(p,c){
                               return c.isSelling === true          &&
                                      c.askingPrice <= buyer.budget &&
                                      c.askingPrice > p.askingPrice ? c : p
                             },{isSelling: true, askingPrice: 0});
console.log(result);

如果您想获取符合我们条件的记录索引,只需将索引参数添加到reduce回调中,并使用初始值“0”,如下所示;

var buyer = {budget:1000},
household = [{  isSelling: false,
              askingPrice: 1500},
             {  isSelling: false,
              askingPrice: 500},
             {  isSelling: true,
              askingPrice: 1000},
             {  isSelling: true,
              askingPrice: 670},
             {  isSelling: true,
              askingPrice: 790},
             {  isSelling: false,
              askingPrice: 1000},
             {  isSelling: true,
              askingPrice: 1200},
             {  isSelling: false,
              askingPrice: 690},
             {  isSelling: true,
              askingPrice: 890},
             {  isSelling: true,
              askingPrice: 1500},
             {  isSelling: true,
              askingPrice: 790},
             {  isSelling: true,
              askingPrice: 900},
             {  isSelling: true,
              askingPrice: 990},
             {  isSelling: false,
              askingPrice: 990}
            ],
   result = household.reduce(function(p,c,i,a){
                               return  a[p].isSelling === false        ||
                                       a[p].askingPrice > buyer.budget ||
                                       c.isSelling === true            &&
                                       c.askingPrice <= buyer.budget   &&
                                       c.askingPrice > a[p].askingPrice ? i : p;  
                             },0);
result = household[result].isSelling === true && household[result].askingPrice <= buyer.budget ? result : -1;
console.log(result);

关于javascript - 在 Javascript 中,如何在满足某些条件的情况下查找数组中具有最高值的特定属性的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38709425/

相关文章:

javascript - 一键添加多个 .appends

javascript - 这叫什么(JS)

javascript - `console.log` 一个 mobx `@observable` 每当它的值改变时

javascript - 如何自动设置DIV元素的高度

c++ - 将 1d 缓冲区重新组织为 2d 数组

algorithm - 最快的无条件排序算法

Javascript 数组,为每个增量创建一个嵌套数组

c++ - 使用 cublas sgemv 时如何跳过 float4 中的第四个元素?

c++ - 如何按一个值属性对海量 map 进行排序?

javascript - 在 React-Redux 中实时重新排序列表的最佳方法?