0

I have array of four JS object:

Object1 = {
    fee: 150
}

Object2 = {
    fee: 300
}

Object3 = {
    fee: 450
}

Object4 = {
    fee: 700
}

I need to find object where value that I provide via input is greater then fee of object but also less than fee of next one. There is also a case where value can not be less than fee of first one and if value is greater than fee of four one I need four one as result.

For example:

value = 50, result = null,

value = 225, result = Object1,

value = 400, result = Object2,

value = 650, result = Object3,

value = 1500, result = Object4,

3
  • What have you tried so far to solve this on your own? And what problem do you have with that attempt? Commented Jan 31, 2022 at 15:56
  • They are sorted? findIndex of the one that violates the "less than next one" criterion, subtract one. Commented Jan 31, 2022 at 15:57
  • 1
    Assuming that the array (of objects) will always be sorted, const getResult = (haystack = objArray, needle = 400) => haystack.reduce((f, i) => (i.fee < needle ? {...i} : f || null) , null); Commented Jan 31, 2022 at 16:07

4 Answers 4

2

You mean something like this?

let obs = [{fee: 150}, {fee: 300}, {fee: 450}, {fee: 700}];

function findObject(currentFee) {
// in case it is not garanteed that obs is sorted, doing this 
// would be necessary beforehand
   return obs.filter(o => o.fee > currentFee)[0]
};
Sign up to request clarification or add additional context in comments.

3 Comments

prepend function to findObject ;-)
or, another way to write it may be: const findObject = feeParam => obs.filter(o => o.fee > feeParam)[0]; ?
@MaikLowrey upps xD Edited
1

Is this what you were looking for?

var objects = [{fee: 150}, {fee: 300}, {fee: 450}, {fee: 700}];

function find(input) {
  for (let i = 0; i < objects.length; i++) {
    if (objects[i + 1]) {
      if (objects[i].fee < input && input < objects[i + 1].fee) return objects[i].fee;    
    }
  }
  return null;
}

console.log(find(701));

4 Comments

It is not working for value under 150
Have you tried with find(701)? The for loop goes till objects.length and within the loop it tries to access objects[i+1] - which means it will try to access index 4 if array-length is 4 and the parameter is above the 4th element's fee.
@jsN00b fixed it I think?
@ShadowLp174, by changing the outer if to if (objects[i + 1]), the code now returns null when parameter is 701. The objective was, IIRC, to return 700 in this case. It does so because it tries to access objects[4] where objects array has only 4 elements (which are at indices: 0, 1, 2, 3). Please try for to terminate at i < objects.length - 1.
1

I used filter with index. My answer is similar to the good answer by @RomanHDev. But my approach was to immediately use the index to decrement the match.

const obs = [{fee: 150}, {fee: 300}, {fee: 450}, {fee: 700}];


findObject = feeParam => obs.filter((o, i) => {  
  if (o.fee >= feeParam) {    
    return obs[i] ?? null;
  }
})[0]

console.log( findObject('25',25) )
console.log( findObject('155',155) )
console.log( findObject('225',225) )
console.log( findObject('301',301) )

Comments

0

Late answer but still an answer:

const array = [
  { fee: 150 },
  { fee: 300 },
  { fee: 450 },
  { fee: 700 },
 ]

function findClosest(input) {
    let diff = undefined
    let closest
    for(const element of array) {
       if(diff === undefined) diff = Math.abs(input - element.fee)
       if(diff >= Math.abs(input - element.fee)) {
         closest = element.fee
         diff = Math.abs(input - element.fee)
       }
       else continue
    } 
  
    return closest

} 

const closest = findClosest(290)
console.log(closest)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.