neoflash neoflash - 4 months ago 9
Javascript Question

In Javascript how to find the object in an array that has the highest valued specific property while meeting some conditions

There is an array called

householdArray
populated with objects that have two properties: the first one is
isSelling
, the value of which is either
true
or
false
. The second one is
askingPrice
the value of which is a number. There is another object called
buyer
that has a
budget
property with a numerical value.

How do I find, out of all the objects that have
isSelling === true
in the
householdArray
which one has the highest
askingPrice
that is within
buyer
's
budget
?

Answer

You can use filters and min max but i would do this job simply in one pass , in O(n) time by utilizing Array.prototype.reduce(). To encourage you using arrow functions I will show you first with the arrows then with conventional function implementation.

Please note that we use an object like {isSelling: true, askingPrice: 0} as initial value to the reduce operation.

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);

And now with a conventional function implementation

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);

And if you would like to get the index of the record that fits our condition you simply add the index argument to the callback of reduce and use an initial value of "0" as follows;

var buyer = {budget:1000},
household = [{  isSelling: false,
              askingPrice: 500},
             {  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  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);

Comments